How to bind an ArrayList or Collection of structures to a Windows Form by using Visual Basic .NET or Visual Basic 2005 (313638)



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 Q313638

SUMMARY

This step-by-step article demonstrates how to bind an ArrayList of structures to a Windows Form. The example includes a Windows Form with three text boxes to display the structure members and four command buttons to move through the ArrayList.

Requirements

Microsoft Visual Basic .NET or Microsoft Visual Basic 2005

back to the top

Discussion of Code

This section describes the important coding concepts that are required to complete this task.

Designing the Structure

A structure that is to be bound to a form must have member accessors. Structure member accessors are virtually the same as the Property Set/Get structure that is found in a class. The structure that is used for the example in this article has three members (only one is shown). A parameterized constructor also is provided but is not required.
Private Structure guitar
    Friend mmake As String
    Friend mmodel As String
    Friend myear As Short

    Public Sub New(ByVal make, ByVal model, ByVal year)
        Me.mmake = make
        Me.mmodel = model
        Me.myear = year
    End Sub

    Public Property make() As String
        Get
            make = mmake
        End Get
        Set(ByVal Value As String)
            mmake = Value
        End Set
    End Property
End Structure
				

back to the top

Adding Structure Instances to an ArrayList

T create instances and add them to the ArrayList, follow these steps:
  1. Declare a variable of the type of the structure.
  2. Declare an ArrayList.
  3. Create instances of the structure, and then add them to the ArrayList.
Private myStruct As guitar
Dim al As New ArrayList()

myStruct = New guitar("Gibson", "Les Paul", 1958)
al.Add(myStruct) 
myStruct = New guitar("Fender", "Jazz Bass", 1964)
al.Add(myStruct)
myStruct = New guitar("Guild", "Bluesbird", 1971)
al.Add(myStruct)
				

back to the top

Binding the Structure Members to Form Controls

After the ArrayList is populated, you can bind the individual members of the structure to Windows Forms controls. To do this, you must call the Add method of the Textbox DataBindings property and pass the property to be bound, the name of the ArrayList, and the member of the structure.
TextBox1.DataBindings.Add("Text", al, "make")
TextBox2.DataBindings.Add("Text", al, "model")
TextBox3.DataBindings.Add("Text", al, "year")
				

back to the top

Providing a Way to Move Through the ArrayList

The final step in the process is to provide a way to move through the ArrayList. To do this, you must use a CurrencyManager. Associate the CurrencyManager with the form's BindingContext, in this case, the ArrayList of structures.
Private cMan As CurrencyManager

cMan = CType(Me.BindingContext(al), CurrencyManager)
				
The CurrencyManager object has a Position property that can be manipulated to iterate over the members of the ArrayList. By adding to, or subtracting from, the current value of Position, you can display different members of the ArrayList on the form.
'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.Count - 1
				

back to the top

Step-by-Step Example

  1. Open a new Windows Application project in Visual Basic .NET or in Visual Basic 2005.
  2. Add three text boxes to Form1, and then arrange the controls horizontally.
  3. Add four command buttons to Form1, and then arrange the controls horizontally.
  4. Change the Text Property of Button1 to Next.
  5. Change the Text Property of Button2 to Previous.
  6. Change the Text Property of Button3 to First.
  7. Change the Text Property of Button4 to Last.
  8. Add the following code to the Declaration Section of Form1:
    Private Structure guitar
        Friend mmake As String
        Friend mmodel As String
        Friend myear As Short
    
        Public Sub New(ByVal make, ByVal model, ByVal year)
            Me.mmake = make
            Me.mmodel = model
            Me.myear = year
        End Sub
    
        Public Property make() As String
            Get
                make = mmake
            End Get
            Set(ByVal Value As String)
                mmake = Value
            End Set
        End Property
        Public Property model() As String
            Get
                model = mmodel
            End Get
            Set(ByVal Value As String)
                mmodel = Value
            End Set
        End Property
        Public Property year() As Short
            Get
                year = myear
            End Get
            Set(ByVal Value As Short)
    
    
            myear = Value
            End Set
        End Property
    End Structure
    
    Private myStruct As guitar
    Private al As New ArrayList()
    Private cMan As CurrencyManager
    					
  9. Add the following code to the Form_Load event:
    myStruct = New guitar("Gibson", "Les Paul", 1958)
    al.Add(myStruct)
    myStruct = New guitar("Fender", "Jazz Bass", 1964)
    al.Add(myStruct)
    myStruct = New guitar("Guild", "Bluesbird", 1971)
    al.Add(myStruct)
    
    cMan = CType(Me.BindingContext(al), CurrencyManager)
    
    TextBox1.DataBindings.Add("Text", al, "make")
    TextBox2.DataBindings.Add("Text", al, "model")
    TextBox3.DataBindings.Add("Text", al, "year")
    					
  10. 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.Count - 1
    End Sub
    					
  11. Build and run the project.
  12. Click the buttons to display different ArrayList elements. Note that you can edit the values of the objects.
back to the top

Using a Collection

You can use a Collection object to store the structures. To test the use of a Collection, locate the following line of code in the Form_Load event procedure:
Private al As New ArrayList()
Replace the preceding line of code with the following code, and then run the example again:
Private al As New Collection()

back to the top

REFERENCES

See the following topic in the Visual Studio .NET or Visual Studio 2005 Online Help documentation:

Consumers of Data on Windows Forms

back to the top

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