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



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • 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

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:
  1. Declare an array.
  2. 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 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:
  1. 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.
  2. 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 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

  1. Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project. The New Project dialog box appears.
  3. Under Project Types, click Visual C++ Projects.

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

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

    By default, Form1 is created.
  6. Add three TextBox controls to Form1, and then arrange these controls horizontally.
  7. Add four Button controls to Form1, and then arrange the controls horizontally.
  8. Right-click button1, and then click Properties.
  9. In the Properties dialog box, change the Text property of the button1 control to Next.
  10. Right-click button2, and then click Properties.
  11. In the Properties dialog box, change the Text property of the button2 control to Previous.
  12. Right-click button3, and then click Properties.
  13. In the Properties dialog box, change the Text property of the button3 control to First.
  14. Right-click button4, and then click Properties.
  15. In the Properties dialog box, change the Text property of the button4 control to Last.
  16. 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:
    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. 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:

    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

  17. Add the following code to the Form1 class:
    private: Guitar *arr[];
    CurrencyManager *currencyManager;
  18. Switch to the Form Designer.
  19. Right-click the Form1 form, and then click Properties.
  20. Click Events, and then double-click the Load event to add the Form1_Load event to your code.
  21. 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");
  22. Switch to the Form Designer.
  23. Double-click Next, and then add the following code to the button1_Click event:
    currencyManager->Position++;
  24. Double-click Previous, and then add the following code to the button2_Click event:
    currencyManager->Position--;
  25. Double-click First, and then add the following code to the button3_Click event:
    currencyManager->Position = 0;
  26. Double-click Last, and then add the following code to the button4_Click event:
    currencyManager->Position = arr->Length - 1;
  27. Press CTRL+SHIFT+B to build the application.
  28. Press CTRL+F5 to run the application.
  29. 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

REFERENCES

For more information about consumers of data on Windows Forms, visit the following Microsoft Developer Network (MSDN) Web site:Back to the top

Modification Type:MajorLast Reviewed:1/4/2006
Keywords:kbConsumer kbWindowsForms kbDataBinding kbCollections kbControl kbHOWTOmaster KB815784 kbAudDeveloper