Microsoft KB Archive/815784

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

Article ID: 815784

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft .NET Framework 1.1

-





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



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

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System::Collections
 * System::Windows::Forms

IN THIS TASK

 * SUMMARY
 * Requirements
 * Design the Structure
 * Add Structure Instances to an Array
 * Bind the Structure Members to Form Controls
 * Browse Through 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 in this article includes a Windows Form that has three text boxes to display the members of the structures. The example also includes four command buttons to browse the array.

Back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual C++ .NET 2003 or Microsoft Visual C++ 2005

This article assumes that you are familiar with the following topics:
 * Intermediate Visual C++ programming concepts

Back to the top

Design the Structure
A structure that you bind to a control must have member accessors. Structure member accessors are almost exactly the same as the property getter functions and the property setter functions that are found in a class. You can implement member accessors by using the property getter function and the property setter function, and you can notify the compiler that these are property functions by using the __property keyword. The sample structure that is used in this article has three members. __gc struct Guitar { private: String *make; String *model; short year; public: Guitar(void) {   };    ~Guitar(void) {   };    Guitar(String *strMake, String *strModel, short shYear) {       make = strMake; model = strModel; year = shYear; }

__property String* get_Make {       return make; }   __property void set_Make(String* val) {       make = (val); }   __property String* get_Model {        return model; }   __property void set_Model(String* val) {       model = (val); }

__property short get_Year {        return year; }   __property void set_Year(short shValue) {       year = shValue; } }; Back to the top

Add Structure Instances to an Array
To create instances of your structure and to add these instances to an array, follow these steps:  Declare an array.  Create instances of the structure, and then add the instances to the array as follows: private: Guitar *arr[];

arr = new Guitar *[3]; arr[0] = new Guitar(S&quot;Gibson&quot;, S&quot;Les Paul&quot;, 1958); arr[1] = new Guitar(S&quot;Fender&quot;, S&quot;Jazz Bass&quot;, 1964); arr[2] = new Guitar(S&quot;Guild&quot;, S&quot;Bluesbird&quot;, 1971); 

Back to the top

Bind the Structure Members to Form Controls
After you populate the array, you can bind the individual members of the structure to Windows Forms controls. To do this, follow these steps:  Call the Add method of the DataBindings property of a TextBox control. The Add method takes the following three parameters:  propertyName: The name of the control property to bind. dataSource: An object that represents the data source.</li> dataMember: The property or list to bind to.</li></ul> </li>  Pass the control property to bind (that is, the Text property), the name of the array (that is, the arr array object), and the member of the structure (that is, the property in the structure) as follows: textBox1->DataBindings->Add(S&quot;Text&quot;, arr, S&quot;make&quot;); textBox2->DataBindings->Add(S&quot;Text&quot;, arr, S&quot;model&quot;); textBox3->DataBindings->Add(S&quot;Text&quot;, arr, S&quot;year&quot;); </li></ol>

Back to the top

Browse the Array
The CurrencyManager class manages a list of binding objects. The CurrencyManager class derives from the BindingManagerBase class. Use the BindingContext object of the form to return either a CurrencyManager object or a PropertyManager object. The actual object that is returned depends on the data source and the data member that are passed to the Item property of the BindingContext object of the form. If the data source is an object that can only return a single property (instead of a list of objects), the type is a PropertyManager object. For example, if you specify a TextBox control as the data source, a PropertyManager object is returned. If the data source is an object that implements IList interfaces, IListSource interfaces, or IBindingList interfaces, a CurrencyManager object is returned.

You can use a CurrencyManager class to browse the array. To do this, pass the arr array object to the Item property BindingContext object of the form. This returns a BindingManagerBase object, and the BindingManagerBase object is cast back to a CurrencyManager object. private: CurrencyManager *currencyManager; currencyManager = NULL; currencyManager = static_cast<CurrencyManager*>(this->BindingContext->get_Item(arr) ); The CurrencyManager class has a Position property that you can change to iterate through the members of the array. By adding to or subtracting from the current value of the Position property, 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
 Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.</li> On the File menu, point to New, and then click Project. The New Project dialog box appears.</li> Under Project Types, click Visual C++ Projects.

Note In Visual Studio 2005, click Visual C++ under Project Types.</li> Under Templates, click Windows Forms Application (.NET).

Note In Visual Studio 2005, click Windows Forms Application under Templates.</li> In the Name text box, type Q815784, and then click OK.

By default, Form1 is created.</li> Add three TextBox controls to Form1, and then arrange these controls horizontally.</li> Add four Button controls to Form1, and then arrange the controls horizontally.</li> Right-click button1, and then click Properties.</li> In the Properties dialog box, change the Text property of the button1 control to Next .</li> Right-click button2, and then click Properties.</li> In the Properties dialog box, change the Text property of the button2 control to Previous .</li> Right-click button3, and then click Properties.</li> In the Properties dialog box, change the Text property of the button3 control to First .</li> <li>Right-click button4, and then click Properties.</li> <li>In the Properties dialog box, change the Text property of the button4 control to Last .</li> <li> Open the Form1.h file, and then add the following code to the Form1 class: __gc struct Guitar { private: String *make; String *model; short year; public: Guitar(void) {   };    ~Guitar(void) {   };    Guitar(String *strMake, String *strModel, short shYear) {       make = strMake; model = strModel; year = shYear; }

__property String* get_Make {       return make; }   __property void set_Make(String* val) {       make = (val); }   __property String* get_Model {        return model; }   __property void set_Model(String* val) {       model = (val); }

__property short get_Year {        return year; }   __property void set_Year(short shValue) {       year = shValue; } }; Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> <li>Expand Configuration Properties, and then click General.</li> <li>Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

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

</li> <li> Add the following code to the Form1 class: private: Guitar *arr[]; CurrencyManager *currencyManager; </li> <li>Switch to the Form Designer.</li> <li>Right-click the Form1 form, and then click Properties.</li> <li>Click Events, and then double-click the Load event to add the Form1_Load event to your code.</li> <li> Add the following code to the Form1_Load event: currencyManager  = NULL; arr = new Guitar *[3]; arr[0] = new Guitar(S&quot;Gibson&quot;, S&quot;Les Paul&quot;, 1958); arr[1] = new Guitar(S&quot;Fender&quot;, S&quot;Jazz Bass&quot;, 1964); arr[2] = new Guitar(S&quot;Guild&quot;, S&quot;Bluesbird&quot;, 1971); currencyManager = static_cast<CurrencyManager*>(this->BindingContext->get_Item(arr) );

textBox1->DataBindings->Add(S&quot;Text&quot;, arr, S&quot;make&quot;); textBox2->DataBindings->Add(S&quot;Text&quot;, arr, S&quot;model&quot;); textBox3->DataBindings->Add(S&quot;Text&quot;, arr, S&quot;year&quot;); </li> <li>Switch to the Form Designer.</li> <li> Double-click Next, and then add the following code to the button1_Click event: currencyManager->Position++; </li> <li> Double-click Previous, and then add the following code to the button2_Click event: currencyManager->Position--; </li> <li> Double-click First, and then add the following code to the button3_Click event: currencyManager->Position = 0; </li> <li> Double-click Last, and then add the following code to the button4_Click event: currencyManager->Position = arr->Length - 1; </li> <li>Press CTRL+SHIFT+B to build the application.</li> <li>Press CTRL+F5 to run the application.</li> <li>Click the various buttons to display different array elements. Notice that you can edit the values of the structure if you want to.</li></ol>

Back to the top

<div class="references_section">