BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Programming
.NET 1.1+

Binding a ComboBox to an Enumeration

Enumerations provide a useful manner in which to create a group of related constants that can be viewed as strings or integer values. When these values are bound to a ComboBox, the user can select an option directly from a list of the enumeration strings.

Enum Class

Enumerations do not implement the interfaces that are required to allow them to be bound to Windows controls. To enable the values from an enumeration to be used for read-only binding, they must be transferred into an array. This conversion can be performed easily using the System.Enum class' methods. In this article we will build a very simple Windows Forms application that uses one of these methods to bind a ComboBox control to an enumeration and retrieve the selected constant from the list.

Creating the Form

To begin, create a new Windows Forms application. Add a ComboBox named "ColourList" and a button named "OKButton" to the form. Change the text of the button to OK. The completed form should be similar to that pictured below:

Bind ComboBox to Enumeration Test Form

Creating the Enumeration

The enumeration that will be bound to the ComboBox will contain eight constants. Each will be a named colour. To create the enumeration, add the following code to the project:

public enum Colour
{
    Black,
    White,
    Red,
    Yellow,
    Green,
    Cyan,
    Blue,
    Magenta
}

Adding the Binding

To obtain an array of values to bind to the ComboBox, the static GetValues method of the Enum class will be used. This method requires that the type of the enumeration to be converted to an array be passed as a parameter. The returned array can then be assigned to the ComboBox's DataSource property.

To bind the list of colours to the control, add the following line of code to the form's Load event. This will create the array and bind it to the ComboBox. Although each value in the list is a Colour, the name of the colour will be displayed, as this is the output of the constant's ToString method.

ColourList.DataSource = Enum.GetValues(typeof(Colour));

If you execute the program at this point, you should see the list of values in the ComboBox control.

Retrieving the Selected Value

The currently selected Colour value can be retrieved using the ComboBox's SelectedItem property. The property returns an object that must be cast to the Colour type to be interrogated fully. NB: If the ComboBox is configured to allow the user to type into the textbox element, they may enter a value that is not in the list. In this case, the SelectedItem property will be null.

To complete the example, double-click the OK button in the form designer to add a Click event. Modify the click event as follows:

private void OKButton_Click(object sender, EventArgs e)
{
    Colour col = (Colour)ColourList.SelectedItem;

    string message = string.Format("{0} ({1})", col.ToString(), (int)col);
    MessageBox.Show(message, "Selection");
}

When the user clicks the OK button, the col variable is populated with the enumeration constant that is selected in the ComboBox. The name and the integer value of the variable are then displayed using a message box. Try running the program to test the results.

31 January 2009