Microsoft KB Archive/313334

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

Article ID: 313334

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q313334



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

IN THIS TASK
SUMMARY Requirements Discussion of Code
 * How to Design the Structure
 * How to Add Structure Instances to an Array
 * How to Bind the Structure Members to Form Controls
 * How to Provide a Means to Navigate the Array

Step-by-Step Example REFERENCES



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(&quot;Gibson&quot;, &quot;Les Paul&quot;, 1958) al(0) = myStruct myStruct = New guitar(&quot;Fender&quot;, &quot;Jazz Bass&quot;, 1964) al(1) = myStruct myStruct = New guitar(&quot;Guild&quot;, &quot;Bluesbird&quot;, 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(&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

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
 Open a new Windows Application project in Visual Basic .NET or Visual Basic 2005. 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:

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 MSDN Web site:

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

 Add four command buttons to Form1; 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.</li>  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 </li>  Add the following code to the Form Load event: myStruct = New guitar(&quot;Gibson&quot;, &quot;Les Paul&quot;, 1958) al(0) = myStruct myStruct = New guitar(&quot;Fender&quot;, &quot;Jazz Bass&quot;, 1964) al(1) = myStruct myStruct = New guitar(&quot;Guild&quot;, &quot;Bluesbird&quot;, 1971) al(2) = myStruct 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 if you want.</li></ol>

back to the top

<div class="references_section">