Microsoft KB Archive/313638

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

Article ID: 313638

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic 2005

-



This article was previously published under Q313638



For a Microsoft C# .NET version of this article, see 313636.

IN THIS TASK
SUMMARY Requirements Discussion of Code
 * Designing the Structure
 * Adding Structure Instances to an ArrayList
 * Binding the Structure Members to Form Controls
 * Providing a Way to Move Through the ArrayList

Step-by-Step Example Using a Collection REFERENCES



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(&quot;Gibson&quot;, &quot;Les Paul&quot;, 1958) al.Add(myStruct) myStruct = New guitar(&quot;Fender&quot;, &quot;Jazz Bass&quot;, 1964) al.Add(myStruct) myStruct = New guitar(&quot;Guild&quot;, &quot;Bluesbird&quot;, 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(&quot;Text&quot;, al, &quot;make&quot;) TextBox2.DataBindings.Add(&quot;Text&quot;, al, &quot;model&quot;) TextBox3.DataBindings.Add(&quot;Text&quot;, al, &quot;year&quot;)

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
 Open a new Windows Application project in Visual Basic .NET or in Visual Basic 2005. Add three text boxes to Form1, and then arrange the controls horizontally. Add four command buttons to Form1, and then arrange the controls horizontally. Change the Text Property of Button1 to Next. Change the Text Property of Button2 to Previous. Change the Text Property of Button3 to First. Change the Text Property of Button4 to Last.</li>  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 </li>  Add the following code to the Form_Load event: myStruct = New guitar(&quot;Gibson&quot;, &quot;Les Paul&quot;, 1958) al.Add(myStruct) myStruct = New guitar(&quot;Fender&quot;, &quot;Jazz Bass&quot;, 1964) al.Add(myStruct) myStruct = New guitar(&quot;Guild&quot;, &quot;Bluesbird&quot;, 1971) al.Add(myStruct)

cMan = CType(Me.BindingContext(al), CurrencyManager)

TextBox1.DataBindings.Add(&quot;Text&quot;, al, &quot;make&quot;) TextBox2.DataBindings.Add(&quot;Text&quot;, al, &quot;model&quot;) TextBox3.DataBindings.Add(&quot;Text&quot;, al, &quot;year&quot;) </li>  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 </li> Build and run the project.</li> Click the buttons to display different ArrayList elements. Note that you can edit the values of the objects.</li></ol>

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