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:
- Declare a variable of the type of the structure.
- Declare an ArrayList.
- 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
- 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.
- 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
- 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")
- 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
- Build and run the project.
- 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