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

Summary

This step-by-step article describes how to bind an ArrayList 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 ArrayList.

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

Add Structure Instances to an ArrayList



To create instances and add them to the ArrayList, follow these steps:
  1. Declare a variable of the type of the structure.
  2. Declare an ArrayList of the type of the structure.
  3. Create instances of the structure and add them to the ArrayList.
private ArrayList al = new ArrayList();

al.Add(new Guitar("Gibson", "Les Paul", 1958));
al.Add(new Guitar("Fender", "Jazz Bass", 1964));
al.Add(new Guitar("Guild", "Bluesbird", 1971));

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 ArrayList, and the property of the object.
textBox1.DataBindings.Add("Text", al, "Make");
textBox2.DataBindings.Add("Text", al, "Model");
textBox3.DataBindings.Add("Text", al, "Year");

Provide a Means to Browse the ArrayList

You can use a CurrencyManager to browse through the ArrayList. To do this, associate the CurrencyManager with the BindingContext of the form (in this case, the ArrayList of structures).
private CurrencyManager currencyManger = null;

currencyManger = (CurrencyManager)this.BindingContext[al];
The CurrencyManager class has a Position property that you can manipulate to iterate over the members of the ArrayList. By adding to, or subtracting from, the current value of Position, you can display different members of the ArrayList on the form.

//Move forward one element.
currencyManager.Position++;
//Move back one element.
currencyManager.Position--;
//Move to the beginning.
currencyManger.Position = 0;
//Move to the end.
currencyManger.Position = al.Count - 1;

Step-by-Step Example

  1. In Visual C# .NET or in Visual C# 2005, create a new Windows Application project. Form1 is created by default.
  2. Add three text boxes to Form1 and arrange the controls horizontally.
  3. Add four buttons to Form1 and arrange the controls horizontally.
  4. Change the Text property of Button1 to Next.
  5. Change the Text property of Button2 to Previous.
  6. Change the Text property of Button3 to First.
  7. Change the Text property of Button4 to Last.
  8. 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 ArrayList al=new ArrayList();
    private CurrencyManager currencyManger = null;
  9. Add the following code to the constructor of the form after the InitializeComponent call (where the code designer has inserted the "TODO: Add any constructor code after InitializeComponent call" comment):
    al.Add(new Guitar("Gibson", "Les Paul", 1958));
    al.Add(new Guitar("Fender", "Jazz Bass", 1964));
    al.Add(new Guitar("Guild", "Bluesbird", 1971));

    currencyManger = (CurrencyManager)this.BindingContext[al];

    textBox1.DataBindings.Add("Text", al, "make");
    textBox2.DataBindings.Add("Text", al, "model");
    textBox3.DataBindings.Add("Text", al, "year");
  10. Switch to view the Forms Designer.
  11. Double-click Next and add the following code to the button1_Click event:
    currencyManger.Position++;
  12. Double-click Previous and add the following code to the button2_Click event:
    currencyManger.Position--;
  13. Double-click First and add the following code to the button3_Click event:
    currencyManger.Position = 0;
  14. Double-click Last and add the following code to the button4_Click event:
    currencyManger.Position = al.Count - 1;
  15. Build and run the project.
  16. Click the command buttons to display different ArrayList 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.

Propriedades

ID do Artigo: 313636 - Última Revisão: 11 de jul de 2008 - Revisão: 1

Comentários