How to bind an array of structures to a Windows Form by using Visual Basic .NET or Visual Basic 2005 (313334)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q313334

SUMMARY

This step-by-step article demonstrates how to bind an array of structures to a Windows Form. The example included here consists of a Windows Form with three text boxes to display the structure members, and four command buttons to navigate the array.

Requirements

Microsoft Visual Basic .NET or Microsoft Visual Basic 2005

back to the top

Discussion of Code

This section highlights the important coding concepts necessary to accomplish this task.

How to Design the Structure

A structure that is to be bound to a Form must have member accessors. Structure member accessors are virtually identical to the Property Set/Get structure found in a class. The structure used for the example in this article has three members (only one is shown here.) A parameterized constructor has also been provided; this is not a requirement.
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

How to Add Structure Instances to an Array

The next step is to create instances and add them to the array. First, declare a variable of the same type as the structure. Then, declare an array, also of the same type as the structure. Next, create instances of the structure and add them to the array.
Private myStruct As guitar
Private al(2) As guitar

myStruct = New guitar("Gibson", "Les Paul", 1958)
al(0) = myStruct
myStruct = New guitar("Fender", "Jazz Bass", 1964)
al(1) = myStruct
myStruct = New guitar("Guild", "Bluesbird", 1971)
al(2) = myStruct
				
back to the top

How to Bind the Structure Members to Form Controls

Now that the array has been populated, you can bind the individual members of the structure to Windows Forms controls. To do this, call the Add method of the Textbox DataBindings property, and then pass the property to be bound, the name of the array, 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

How to Provide a Means to Navigate the Array

The final step in the process is to provide a way to navigate through the array. This is accomplished through the use of a CurrencyManager object. Associate the CurrencyManager with the Form's BindingContext property -- in this case, the array 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 array. By adding to, or subtracting from, the current value of Position, different members of the array can be displayed 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.Length - 1
				
back to the top

Step-by-Step Example

  1. Open a new Windows Application project in Visual Basic .NET or Visual Basic 2005.
  2. Add three textboxes to Form1. Arrange the controls horizontally.

    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:
  3. Add four command buttons to Form1; 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 cMan As CurrencyManager
    Private al(2) As guitar
    					
  9. Add the following code to the Form Load event:
    myStruct = New guitar("Gibson", "Les Paul", 1958)
    al(0) = myStruct
    myStruct = New guitar("Fender", "Jazz Bass", 1964)
    al(1) = myStruct
    myStruct = New guitar("Guild", "Bluesbird", 1971)
    al(2) = 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.Length - 1
    End Sub
    					
  11. Build and run the project.
  12. Click the buttons to display different array elements.

    Note that you can edit the values of the objects if you want.
back to the top

REFERENCES

Search on the following topic in 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 kbinfo KB313334 kbAudDeveloper