Populating a Combo Box with an Enumeration

I though I’d share some code. If you aren’t a .net programmer, this is probably pretty useless for you.

The need was to come up with an easy way to take an enumeration list and turn it into a user interface element. Combo boxes seemed like a good fit UI wise but there was no simple way to bind the list in the box to an enumeration. It seems an easy match since an enumeration is a numeric key and a text representation, which is often what you have in a combo.

Making the list of values is pretty simple, but I think the clever bit of this class is how its used, via a static method that acts as a factory for the actual object that contains the data needed for the combo. This keeps your code clean as only one command is needed to get, populate, and bind the combo box.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sigs_C_Libraries

{
    // a class that turns an enumeration into a binable list for drop down boxes
    // it only works on enumeraitons that use the default integer for its backing field
    // or those where you can implicitly conver the backing field to an intiger
    // it allows lets you bind the combo's directly to a property of the enumerations type
    public class EnumList : List<EnumItem>
    {
        // you can use the constructor directly if you like, but the static factory method is easier
        public EnumList(Type EnumType, Boolean AddBlank)
        {
            string[] NameList = Enum.GetNames(EnumType);
            int[] ValueList = (int[])Enum.GetValues(EnumType);

            if (AddBlank)
                this.Add(new EnumItem("", -1));

            for (int counter = 0; counter < NameList.Length; counter++)
            {
                this.Add(new EnumItem(NameList[counter], ValueList[counter]));
            }
        }

        // what we have here is a static method you can call that acts a bit like a factory method
        // it creates a new object of this class type, makes it the combos data source, and sets the binding info for you
        // no need to track the actual list, its held in memory by the ref in the combo box and will collect when not needed
        // this is the way the class is intended to be used
        // EnumList.DropDownEnumSetup(typeof(MyEnum),MyComboBox,true);
        public static void DropDownEnumSetup(Type enumeration, System.Windows.Forms.ComboBox combo, Boolean AddBlank)
        {
            combo.DataSource = new EnumList(enumeration, AddBlank);
            combo.DisplayMember = "Name";
            combo.ValueMember = "Value";
        }
    }

    // This little guy is the item that goes in our list
    // I took the liberty of defining it in the same file cause its tidy that way
    public class EnumItem
    {
        public string Name { get; set; }
        public int Value { get; set; }

        public EnumItem(string name, int value)
        {
            Name = name;
            Value = value;
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *