Posted by: Cirilo Meggiolaro | 12/15/2008

Tip of the day #62 – Populating a combobox control with enumerator items

Today’s tip is quick but useful. Let’s check how to populate a combo box with all enumerator options based only on the enumerator type. The goal here is not to check how to add items to a combo but to check how to use two main methods available under the Enum class.

How to…

The static Enum class provides some methods to help you retrieve values, names, parse an item and so on. I am going to focus today in two methods: GetNames and GetValues. Let’s check the methods signatures:

public static string[] GetNames(Type enumType);

public static Array GetValues(Type enumType);

As you may realize the GetNames method retrieves an array of string that stores the enumerator items name while the GetValues method retrieves a basic Array with a list of all enumerator items value.

Retrieving enumerator items

  • Let’s assume that we have an enumerator named MyEnum with two options: Foo and Bar:

public enum MyEnum
{
    Foo,
    Bar
}

  • The first task you need to perform is to retrieve a list of values using the GetValues method:

/// Gets a list of values based on the enumerator type
Enum.GetValues(typeof(MyEnum));

  • The second task you must perform is to get the name based on the enumerator item value:

/// Gets the name of an enumerator item based on enumerator type and value
Enum.GetName(typeof(MyEnum), enumeratorItemValue);

  • For this example I am using a helper class to store text and value for each combo box item like the following code snippet. It means that to display correctly you have to define the DisplayMember property to “ItemText” and the ValueMember property to “ItemValue”:

public class DropDownItem
{
    public DropDownItem(string text, int value)
    {
        ItemText = text;
        ItemValue = value;
    }

    public int ItemValue { get; set; }
    public string ItemText { get; set; }
}

  • You may create a loop to populate the combobox similar to the code snippet below:

foreach (object enumOption in Enum.GetValues(typeof(MyEnum)))
{
    cbo.Items.Add(new DropDownItem(Enum.GetName(typeof(MyEnum), enumOption), (int)enumOption));
}

Identifying the selected item

Once you have the enumerator item value stored as the combobox item value, identify the item that is currently selected is just a matter of convert.

  • Check if any item is selected on the combobox;
  • Convert the SelectedItem object to our helper class DropDownItem;
  • The ItemValue contains the enumerator value, so you just need to convert to a MyEnum type. The following code resumes these three steps:

(MyEnum)((DropDownItem)cbo.SelectedItem).ItemValue;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: