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

This article was previously published under Q313640
This article has been archived. It is offered "as is" and will no longer be updated.
For a Microsoft Visual C# .NET version of this article, see 313634.
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 PropertyEnd 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 CurrencyManagercMan = 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

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

    NoteYou 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:
  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 PropertyEnd 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 As New ArrayList()Private cMan As CurrencyManager					
  8. 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")					
  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 += 1End SubPrivate Sub Button2_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button2.Click        cMan.Position -= 1End SubPrivate Sub Button3_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button3.Click        cMan.Position = 0End SubPrivate Sub Button4_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button4.Click        cMan.Position = al.Count - 1End 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.

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:
  1. Locate the following code in the Form_Load event procedure:
    Private al As New ArrayList()					
  2. Replace the above code with the following code:
    Private al As New Collection()					
  3. Save the changes to the project, and then run the project again.
REFERENCES
For more information, see the "Consumers of Data on Windows Forms" topic in the Visual Studio 2005 or Visual Studio .NET Online Help documentation.
Properties

Article ID: 313640 - Last Review: 02/27/2014 21:11:00 - Revision: 4.5

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • kbnosurvey kbarchive kbvs2005swept kbvs2005applies kbhowtomaster KB313640
Feedback