Re-coded using LINQ. Stores the actual enum values in the combobox.
public static void SetEnumValues<T>(ComboBox cb)
{
Type t = typeof(T);
if (!t.IsEnum)
throw new ArgumentException("Only Enum types can be set");
cb.DisplayMember = "Key";
cb.ValueMember = "Value";
cb.DataSource = Enum.GetValues(t).Cast<T>().Select(
val =>
{
string desc = Enum.GetName(t, val);
DescriptionAttribute attr =
((DescriptionAttribute[])
t.GetField(desc).
GetCustomAttributes(typeof(DescriptionAttribute), false)).
FirstOrDefault();
return new KeyValuePair<string, T>(
attr != null && !string.IsNullOrEmpty(attr.Description) ? attr.Description : desc,
val);
}).ToList();
}
Call with:
SetEnumValues<MyEnum>(myComboBox);
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.