Microsoft KB Archive/313639

= How to bind an array of objects to a Windows form by using Visual Basic .NET or Visual Basic 2005 =

Article ID: 313639

Article Last Modified on 5/11/2007

-

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 Q313639



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

IN THIS TASK
SUMMARY Requirements Code Description
 * Design the Class
 * Add Class Instances to an Array
 * Bind the Object Properties to Form Controls
 * Provide a Means to Browse Through the Array

Step-by-Step Example REFERENCES



SUMMARY
This step-by-step article describes how to bind an array of objects to a Windows form. 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 Array class.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, skills, knowledge, and service packs that you need:
 * Microsoft Visual Basic .NET or Microsoft Visual Basic 2005
 * Intermediate understanding of Visual Basic programming concepts

back to the top

Code Description
This section highlights the coding concepts that are necessary to accomplish this task.

back to the top

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 back to the top

Add Class Instances to an Array
The next step is to create the objects and add them to the Array class: Dim al(2) As guitar

al(0) = new guitar(&quot;Gibson&quot;, &quot;Les Paul&quot;, 1958) al(1) = new guitar(&quot;Fender&quot;, &quot;Jazz Bass&quot;, 1964) al(2) = new guitar(&quot;Guild&quot;, &quot;Bluesbird&quot;, 1971) back to the top

Bind the Object Properties to Form Controls
After the Array 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, you must pass the control property that will be bound, the name of the Array, and the property of the object. 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

Provide a Means to Browse Through the Array
In the final step of this process, you use a CurrencyManager class to browse through the Array. To do this, associate the CurrencyManager with the BindingContext of the form (in this case, the Array). 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 Array. By adding to, or by subtracting from, the current value of Position, you can display different members of the Array 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.Length - 1 back to the top

Step-by-Step Example
 Open a new Windows Application project in Visual Basic .NET or Visual Basic 2005. Form1 is added to the project by default.

Note The code should be changed in Visual Basic 2005. If you create a new form named Form1 in Visual Basic 2005, you have a Form1.vb file for your code and a Form1.Designer.vb file that contains the automatically generated section. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This prevents the designer-emitted code from being interspersed with your code.

For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

For more information about partial classes and the Windows Forms Designer, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

 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:

  Add the following code to the declaration section of Form1: Private al(2) As guitar Private cMan As CurrencyManager </li>  Add the following code to the Form_Load event: al(0) = new guitar(&quot;Gibson&quot;, &quot;Les Paul&quot;, 1958) al(1) = new guitar(&quot;Fender&quot;, &quot;Jazz Bass&quot;, 1964) al(2) = new guitar(&quot;Guild&quot;, &quot;Bluesbird&quot;, 1971)

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.Length - 1 End Sub </li> Build and run the project.</li> Click the buttons to display different array elements. Note that you can edit the values of the objects.</li></ol>

back to the top

<div class="references_section">