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

Summary

This step-by-step article shows you how to bind an array of objects to a Windows form. The example consists of a Windows form with three text boxes to display the object properties, and four command buttons to browse the array.


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

Design the Class

A class that is to be bound to a form must have property accessors. Any property that is to be bound must have Property Set and Property Get methods. The class 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 class guitar
{
private string make;
private string model;
private short year;

public guitar()
{
}

public guitar(string make, string model, short year)
{
make=Make;
model=Model;
year=Year;
}

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


}

Add Class Instances to an Array


To create instances and add them to the array, follow these steps:
  1. Declare an array.
  2. Create instances of the class and add them to the array.
private guitar[] arr=new guitar[3];

arr[0] = new guitar("Gibson", "Les Paul", 1958);
arr[1] = new guitar("Fender", "Jazz Bass", 1964);
arr[2] = new guitar("Guild", "Bluesbird", 1971);

Bind the Object Properties to Windows Forms Controls

After the array 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 property of the object.
textBox1.DataBindings.Add("Text", arr, "make");
textBox2.DataBindings.Add("Text", arr, "model");
textBox3.DataBindings.Add("Text", arr, "year");

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).
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;

Step-by-Step Example

  1. In Visual C# .NET or Visual C# 2005, create a new Windows Application project. Form1 is created by default.
  2. Add a class to the project.
  3. Replace the code in Form1.cs with the following:
    public class guitar
    {
    private string make;
    private string model;
    private short year;

    public guitar()
    {
    }

    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;
    }
    }
    }
    Note The code should be changed in Visual Studio 2005. When you create a Windows Forms project, Visual C# adds one form to the project by default. This form is named Form1. 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 dragging and dropping controls from the Toolbox. For more information about the Windows Forms Designer in Visual C# 2005, visit the following Microsoft Web site:

  4. Close the Form1.cs code window, and switch to the Form Designer.
  5. Add three text boxes to Form1 and arrange the controls horizontally.
  6. Add four buttons to Form1 and arrange the controls horizontally.
  7. Change the Text property of Button1 to Next.
  8. Change the Text property of Button2 to Previous.
  9. Change the Text property of Button3 to First.
  10. Change the Text property of Button4 to Last.
  11. Add the following code to the Form1 class:
    private guitar[] arr=new guitar[3];
    private CurrencyManager currencyManager=null;
  12. Switch to the Form Designer, right-click the form, and then click Properties.
  13. Click the Events icon, then double-click the Load event to add the Form1_Load event to your code.
  14. Add the following code to the Form1_Load event:
    arr[0] = new guitar("Gibson", "Les Paul", 1958);
    arr[1] = new guitar("Fender", "Jazz Bass", 1964);
    arr[2] = new guitar("Guild", "Bluesbird", 1971);

    currencyManager = (CurrencyManager)this.BindingContext[arr];

    textBox1.DataBindings.Add("Text", arr, "make");
    textBox2.DataBindings.Add("Text", arr, "model");
    textBox3.DataBindings.Add("Text", arr, "year");
  15. Switch to view the Form Designer.
  16. Double-click Next and add the following code to the button1_Click event:
    currencyManager.Position++;
  17. Double-click Previous and add the following code to the button2_Click event:
    currencyManager.Position--;
  18. Double-click First and add the following code to the button3_Click event:
    currencyManager.Position = 0;
  19. Double-click Last and add the following code to the button4_Click event:
    currencyManager.Position = arr.Length - 1;
  20. Build and run the project.
  21. Click the command buttons to display different array elements. Note that you can edit the values of the objects if desired.

References

For more information, see the "Consumers of Data on Windows Forms" topic in the Visual Studio .NET Online Help.

Proprietăți

ID articol: 313635 - Ultima examinare: 11 iul. 2008 - Revizie: 1

Feedback