Microsoft KB Archive/313335

= How to bind an array of structures to a Windows Form by using Visual C# =

Article ID: 313335

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-



This article was previously published under Q313335



For a Microsoft Visual Basic .NET version of this article, see 313334.

IN THIS TASK
SUMMARY Requirements
 * Design the Structure
 * Add Structure Instances to an Array
 * Bind the Structure Members to Form Controls
 * Provide a Means to Browse the Array

Step-by-Step Example REFERENCES



SUMMARY
This step-by-step article describes how to bind an array of structures to a Windows form. The example consists of a Windows form with three text boxes to display the structure members, and four command buttons to browse the array.

back to the top

Requirements
The following items describe the recommended hardware, software, network infrastructure, skills, knowledge, and service packs that you need:
 * Microsoft Visual C# .NET or Microsoft Visual C# 2005
 * An intermediate understanding of C# programming concepts

back to the top

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 and PROPERTY GET structures that are found in a class. The structure that is used for the example in this article has three members (only one is shown here.) A parameterized constructor has also been provided, but is not a requirement. public struct Guitar {   private string make; private string model; private short year; public Guitar(string Make, string Model, short Year) {   make = Make; model = Model; year = Year; }

public string Make {       get {        return make; }   set {       make = value; }   } } back to the top

Add Structure Instances to an Array
To create instances and add them to the array, follow these steps:
 * 1) Declare a variable of the type of the structure.
 * 2) Declare an array of the type of the structure.
 * 3) Create instances of the structure and add them to the array.

private Guitar[] arr =new Guitar[3];

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

Bind the Structure Members to Form Controls
After the ArrayList has been populated, you can bind the individual properties of the object to Windows Forms controls. To do this, follow these steps:
 * 1) Call the Add method of the Textbox DataBindings property.
 * 2) Pass the control property that is to be bound, the name of the array, and the member of the structure.

textBox1.DataBindings.Add(&quot;Text&quot;, arr, &quot;Make&quot;); textBox2.DataBindings.Add(&quot;Text&quot;, arr, &quot;Model&quot;); textBox3.DataBindings.Add(&quot;Text&quot;, arr, &quot;Year&quot;);

back to the top

Provide a Means to Browse the Array
You can use a CurrencyManager to browse through the array. To do this, associate the CurrencyManager with the BindingContext of the form (in this case, the array of structures). private CurrencyManager currencyManager = null;

currencyManager = (CurrencyManager)this.BindingContext[arr]; The CurrencyManager class has a Position property that you can manipulate to iterate over the members of the array. By adding to, or subtracting from, the current value of Position, you can display different members of the array on the form. //Move forward one element. currencyManager.Position++; //Move back one element. currencyManager.Position--; //Move to the beginning. currencyManager.Position = 0; //Move to the end. currencyManager.Position = arr.Length - 1; back to the top

Step-by-Step Example
 In Visual C# .NET or Visual C# 2005, create a new Windows Application project. Form1 is created by default. Add three text boxes to Form1 and arrange the controls horizontally. Add four buttons to Form1 and 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 Form1 class: public struct Guitar {   private string make; private string model; private short year; public Guitar(string Make, string Model, short Year) {   make=Make; model=Model; year=Year; }

public string Make {       get {        return make; }   set {       make = value; }   }    public string Model {   get {        return model; }   set {       model = value; }   }

public short Year {   get {        return year; }   set {       year = value; }   } }

private Guitar[] arr = new Guitar[3]; private CurrencyManager currencyManager = null; Note This code example should be changed in Visual Studio 2005. By default, Visual C# adds one form to a project and calls the form Form1 when you create a Windows Forms project. The two files that represent the form are named Form1.cs and Form1.designer.cs. You write your code in Form1.cs. The designer.cs file is where the Windows Forms Designer writes the code that implements all the actions that you performed by adding controls to the form.

For more information about the Windows Forms Designer in Visual C# 2005, visit the following Microsoft Web site:

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

</li>  Add the following code to the constructor of the form after the InitializeComponent call (where the code designer has inserted the &quot;TODO: Add any constructor code after InitializeComponent call&quot; comment): arr[0] = new Guitar(&quot;Gibson&quot;, &quot;Les Paul&quot;, 1958); arr[1] = new guitar(&quot;Fender&quot;, &quot;Jazz Bass&quot;, 1964); arr[2] = new guitar(&quot;Guild&quot;, &quot;Bluesbird&quot;, 1971);

currencyManager = (CurrencyManager)this.BindingContext[arr]; textBox1.DataBindings.Add(&quot;Text&quot;, arr, &quot;Make&quot;); textBox2.DataBindings.Add(&quot;Text&quot;, arr, &quot;Model&quot;); textBox3.DataBindings.Add(&quot;Text&quot;, arr, &quot;Year&quot;); </li> Switch to view the Form Designer.</li>  Double-click Next and add the following code to the button1_Click event: currencyManager.Position++; </li>  Double-click Previous and add the following code to the button2_Click event: currencyManager.Position--; </li>  Double-click First and add the following code to the button3_Click event: currencyManager.Position = 0; </li>  Double-click Last and add the following code to the button4_Click event: currencyManager.Position = arr.Length - 1; </li> Build and run the project.</li> Click the command buttons to display different array elements. Note that you can edit the values of the objects if desired.</li></ol>

back to the top

<div class="references_section">