If you have a range of values in your program, and you want the user to select one of them, the most useful (to him) is a human readable string. Unfortunately, the most useful (to you) is an enum value, and the most commonly available is the enum value name. Which means that the value name has to be carefully selected to fit both the human readable requirement, and the compiler requirement of "no special characters". Such as space.
The alternative is to load a combo box with a set of fixed strings, and then decode them into your enum values later. Which is something I avoid, as I am sure to miss one when I add options to the enum.
This Tip presents a way to fill a ComboBox with human readable strings, and tie them to enum values.
Create your enum, and give human readable Description attributes:
public enum MyEnum
{
[Description("The first")]
the1st = 1,
[Description("The second one")]
the2nd = 8000,
[Description("Another one!")]
final = -1,
}
Then include this static method in your code:
public static void SetEnumValues(ComboBox cb, Type t)
{
if (!t.IsEnum)
{
throw new ArgumentException("Only Enum types can be set");
}
List<KeyValuePair<string, int>> list = new List<KeyValuePair<string, int>>();
foreach (int i in Enum.GetValues(t))
{
string name = Enum.GetName(t, i);
string desc = name;
FieldInfo fi = t.GetField(name);
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes.Length > 0)
{
string s = attributes[0].Description;
if (!string.IsNullOrEmpty(s))
{
desc = s;
}
}
list.Add(new KeyValuePair<string, int>(desc, i));
}
cb.DisplayMember = "Key";
cb.ValueMember = "Value";
cb.DataSource = list;
}
Call the method to set the values into your ComboBox:
myComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
SetEnumValues(myComboBox, typeof(MyEnum));
The CombBox will now show the values:
The first
The second one
Another one!
You can now use the values as you wish in your ComboBox events:
private void myComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
Console.WriteLine(myComboBox.Text);
Console.WriteLine(myComboBox.SelectedValue);
MyEnum me = (MyEnum)myComboBox.SelectedValue;
Console.WriteLine(me.ToString());
}
Will display:
The first
1
the1st
The second one
8000
the2nd
Another one!
-1
final