How to bind an array of objects to a Windows form by using Visual Basic .NET or Visual Basic 2005 (313639)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic 2005

This article was previously published under Q313639

SUMMARY

This step-by-step article describes how to bind an array of objects to a Windows form. The example in this article includes a Windows form with three text boxes to display the object properties. The example also includes four command buttons to browse through the Array class.

back to the top

Requirements

The following list outlines the recommended hardware, software, network infrastructure, skills, knowledge, and service packs that you need:
  • Microsoft Visual Basic .NET or Microsoft Visual Basic 2005
  • Intermediate understanding of Visual Basic programming concepts
back to the top

Code Description

This section highlights the coding concepts that are necessary to accomplish this task.

back to the top

Design the Class

A class that will be bound to a form must have property accessors. Any property that will be bound must have the Property Set and the Property Get methods. The class that is used for the example in this article has three properties (only one is shown in this section). The example in this article also includes a parameterized constructor, although this is not required.
Public Class guitar
    Private m_make As String
    Private m_model As String
    Private m_year As Short

    Public Sub New(ByVal make, ByRef model, ByVal year)
        Me.make = make
        Me.model = model
        Me.year = year
    End Sub
    Public Property make() As String
        Get
            Return m_make
        End Get
        Set(ByVal Value As String)
            m_make = Value
        End Set
    End Property 
End Class
				
back to the top

Add Class Instances to an Array

The next step is to create the objects and add them to the Array class:
Dim al(2) As guitar

al(0)  = new guitar("Gibson", "Les Paul", 1958)
al(1)  = new guitar("Fender", "Jazz Bass", 1964)
al(2)  = new guitar("Guild", "Bluesbird", 1971)
				
back to the top

Bind the Object Properties to Form Controls

After the Array is populated, you can bind the individual properties of the object to Windows Forms controls. To do this, call the Add method of the DataBindings property of the TextBox control. In addition, you must pass the control property that will be bound, the name of the Array, and the property of the object.
textBox1.DataBindings.Add("Text", al, "make")
textBox2.DataBindings.Add("Text", al, "model")
textBox3.DataBindings.Add("Text", al, "year")
				
back to the top

Provide a Means to Browse Through the Array

In the final step of this process, you use a CurrencyManager class to browse through the Array. To do this, associate the CurrencyManager with the BindingContext of the form (in this case, the Array).
Private cMan As CurrencyManager

cMan = CType(Me.BindingContext(al), CurrencyManager)
				
The CurrencyManager class has a Position property that you can manipulate to iterate over the members of the Array. By adding to, or by subtracting from, the current value of Position, you can display different members of the Array on the form. For example:
'Move forward one element.
cMan.Position += 1
'Move back one element.
cMan.Position -= 1
'Move to the beginning.
cMan.Position = 0
'Move to the end.
cMan.Position = al.Length - 1
				
back to the top

Step-by-Step Example

  1. Open a new Windows Application project in Visual Basic .NET or Visual Basic 2005. Form1 is added to the project by default.

    Note The code should be changed in Visual Basic 2005. If you create a new form named Form1 in Visual Basic 2005, you have a Form1.vb file for your code and a Form1.Designer.vb file that contains the automatically generated section. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This prevents the designer-emitted code from being interspersed with your code.

    For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Web site: For more information about partial classes and the Windows Forms Designer, visit the following Microsoft Web site:
  2. Add a class to the project.
  3. Replace the code in Class1.vb with the following code:
    Public Class guitar
        Private m_make As String
        Private m_model As String
        Private m_year As Short
    
        Public Sub New(ByVal make, ByRef model, ByVal year)
            Me.make = make
            Me.model = model
            Me.year = year
        End Sub
        Public Property make() As String
            Get
                Return m_make
            End Get
            Set(ByVal Value As String)
                m_make = Value
            End Set
        End Property
        Public Property model() As String
            Get
                Return m_model
            End Get
            Set(ByVal Value As String)
                m_model = Value
            End Set
        End Property
        Public Property year() As Short
            Get
                Return m_year
            End Get
            Set(ByVal Value As Short)
                m_year = Value
            End Set
        End Property
    End Class
    					
  4. Add three TextBox controls to Form1, and then arrange the controls horizontally.
  5. Add four Button controls to Form1, and then arrange the controls horizontally.
  6. Change the Text property of the buttons as follows:
    ButtonText
    Button1Next
    Button2Previous
    Button3First
    Button4Last

  7. Add the following code to the declaration section of Form1:
    Private al(2) As guitar
    Private cMan As CurrencyManager
    					
  8. Add the following code to the Form_Load event:
    al(0)  = new guitar("Gibson", "Les Paul", 1958)
    al(1)  = new guitar("Fender", "Jazz Bass", 1964)
    al(2)  = new guitar("Guild", "Bluesbird", 1971)
    
    cMan = CType(Me.BindingContext(al), CurrencyManager)
    
    TextBox1.DataBindings.Add("Text", al, "make")
    TextBox2.DataBindings.Add("Text", al, "model")
    TextBox3.DataBindings.Add("Text", al, "year")
    					

  9. Add the following code after the Form_Load event:
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
            cMan.Position += 1
    End Sub
    
    Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
            cMan.Position -= 1
    End Sub
    
    Private Sub Button3_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button3.Click
            cMan.Position = 0
    End Sub
    
    Private Sub Button4_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button4.Click
            cMan.Position = al.Length - 1
    End Sub
    					
  10. Build and run the project.
  11. Click the buttons to display different array elements. Note that you can edit the values of the objects.
back to the top

REFERENCES

For more information, refer to the "Consumers of Data on Windows Forms" topic in the Microsoft Visual Studio .NET or Visual Studio 2005 Online Help documentation.

back to the top

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbHOWTOmaster KB313639 kbAudDeveloper