SUMMARY
This step-by-step article describes how to bind an
ArrayList of objects to a Microsoft Windows Form in Microsoft Visual Basic 2005 or in Microsoft Visual Baisc .NET. 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
ArrayList.
Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
- Visual Basic 2005 or Visual Basic .NET
This article assumes that you have an intermediate understanding of Visual Basic programming concepts.
Code description
This section highlights the coding concepts that are necessary to accomplish this task.
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
Add class instances to an ArrayList
The next step is to create objects and add them to the
ArrayList class:
Private al As New ArrayList()
al.Add(New guitar("Gibson", "Les Paul", 1958))
al.Add(New guitar("Fender", "Jazz Bass", 1964))
al.Add(New Guitar("Guild", "Bluesbird", 1971))
Bind the object properties to form controls
After the
ArrayList 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, pass the control property that will be bound, the name of the
ArrayList, and the property of the object.
TextBox1.DataBindings.Add("Text", al, "Make")
TextBox2.DataBindings.Add("Text", al, "Model")
TextBox3.DataBindings.Add("Text", al, "Year")
Provide a means to browse through the ArrayList
In the final step of this process, you use a
CurrencyManager class to browse through the
ArrayList. To do this, associate the
CurrencyManager with the
BindingContext of the form (in this case, the
ArrayList).
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
ArrayList. By adding to, or by subtracting from, the current value of
Position, you can display different members of the
ArrayList 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.Count - 1
Step-by-step example
- Create a new Windows Application project in Visual Basic 2005 or in Visual Basic .NET. Form1 is added to the project by default.
Note
You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows
Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You
write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms
Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the
designer-generated code from being interspersed with your code.
For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network
(MSDN) Web site:
For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:
- Add a class to the project.
- 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
- Add three TextBox controls to Form1, and then arrange the controls horizontally.
- Add four Button controls to Form1, and then arrange the controls horizontally.
- Change the Text property of the buttons as follows:
|
Button1 | Next |
Button2 | Previous |
Button3 | First |
Button4 | Last |
- Add the following code to the declaration section of Form1:
Private al As New ArrayList()
Private cMan As CurrencyManager
- Add the following code to the Form_Load event:
al.Add(New guitar("Gibson", "Les Paul", 1958))
al.Add(New guitar("Fender", "Jazz Bass", 1964))
al.Add(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")
- 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 array elements. Note that you can edit the values of the objects.
Use a Collection object
You can also use a
Collection object to store the structures. To test the use of a
Collection object, follow these steps:
- Locate the following code in the Form_Load event procedure:
Private al As New ArrayList()
- Replace the above code with the following code:
Private al As New Collection()
- Save the changes to the project, and then run the project again.