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 topRequirements
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 topA 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 topAdd 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"Gibson", S"Les Paul", 1958);
arr[1] = new Guitar(S"Fender", S"Jazz Bass", 1964);
arr[2] = new Guitar(S"Guild", S"Bluesbird", 1971);
Back to the topBind 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.
- dataMember: The property or list to bind to.
- 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"Text", arr, S"make");
textBox2->DataBindings->Add(S"Text", arr, S"model");
textBox3->DataBindings->Add(S"Text", arr, S"year");
Back to the topBrowse 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 topStep-by-Step Example
- Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
- On the File menu, point to
New, and then click Project. The New
Project dialog box appears.
- Under Project Types, click Visual
C++ Projects.
Note In Visual Studio 2005, click Visual C++ under Project Types. - Under Templates, click Windows
Forms Application (.NET).
Note In Visual Studio 2005, click Windows Forms Application under Templates. - In the Name text box, type
Q815784, and then click OK.
By
default, Form1 is created. - Add three TextBox controls to Form1, and then arrange these
controls horizontally.
- Add four Button controls to Form1, and then arrange the
controls horizontally.
- Right-click button1, and then click Properties.
- In the Properties dialog box, change the Text property of the
button1 control to Next.
- Right-click button2, and then click Properties.
- In the Properties dialog box, change the Text property of the
button2 control to Previous.
- Right-click button3, and then click Properties.
- In the Properties dialog box, change the Text property of the
button3 control to First.
- Right-click button4, and then click Properties.
- In the Properties dialog box, change the Text property of the
button4 control to Last.
- 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:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the name of the project. - Expand Configuration Properties, and then click General.
- 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.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site: - Add the following code to the Form1 class:
private: Guitar *arr[];
CurrencyManager *currencyManager;
- Switch to the Form Designer.
- Right-click the Form1 form, and then click
Properties.
- Click Events, and then double-click the
Load event to add the Form1_Load event to
your code.
- Add the following code to the Form1_Load
event:
currencyManager = NULL;
arr = new Guitar *[3];
arr[0] = new Guitar(S"Gibson", S"Les Paul", 1958);
arr[1] = new Guitar(S"Fender", S"Jazz Bass", 1964);
arr[2] = new Guitar(S"Guild", S"Bluesbird", 1971);
currencyManager = static_cast<CurrencyManager*>(this->BindingContext->get_Item(arr) );
textBox1->DataBindings->Add(S"Text", arr, S"make");
textBox2->DataBindings->Add(S"Text", arr, S"model");
textBox3->DataBindings->Add(S"Text", arr, S"year");
- Switch to the Form Designer.
- Double-click Next, and then add the
following code to the button1_Click event:
currencyManager->Position++;
- Double-click Previous, and then add the
following code to the button2_Click event:
currencyManager->Position--;
- Double-click First, and then add the
following code to the button3_Click event:
currencyManager->Position = 0;
- Double-click Last, and then add the
following code to the button4_Click event:
currencyManager->Position = arr->Length - 1;
- Press CTRL+SHIFT+B to build the application.
- Press CTRL+F5 to run
the application.
- Click the various buttons to display different array
elements. Notice that you can edit the values of the structure if you want
to.
Back to the
top