I have an enum, example:
enum MyEnum
{
My_Value_1,
My_Value_2
}
With :
comboBox1.DataSource = Enum.GetValues(typeof(MyEnum)
Try this...
comboBox1.DataSource = Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>()
.Select(e => new { Value = e, Description = e.ToString().Replace("_"," ") })
.ToList();
comboBox1.DisplayMember = "Description";
comboBox1.ValueMember = "Value";
...although, I would be more inclined to use a "Description" attribute (as per Steve Crane's answer).
If you're using .NET 3.5, you could add this extension class:
public static class EnumExtensions {
public static List<string> GetFriendlyNames(this Enum enm) {
List<string> result = new List<string>();
result.AddRange(Enum.GetNames(enm.GetType()).Select(s => s.ToFriendlyName()));
return result;
}
public static string GetFriendlyName(this Enum enm) {
return Enum.GetName(enm.GetType(), enm).ToFriendlyName();
}
private static string ToFriendlyName(this string orig) {
return orig.Replace("_", " ");
}
}
And then to set up your combo box you'd just do:
MyEnum val = MyEnum.My_Value_1;
comboBox1.DataSource = val.GetFriendlyNames();
comboBox1.SelectedItem = val.GetFriendlyName();
This should work with any Enum. You'd have to make sure you have a using statement for the namespace that includes the EnumExtensions class.
I like Kelsey's answer although I would use another variable other than 'e' such as 'en' so the answer can be used in event handlers with less hassle; 'e' in event handlers tends to be the EventArgs
argument.
As for the LINQ and IEnumerable
approach, it seems to me to be more complex and difficult to adapt for non-WPF ComboBox
es with .NET 3.5
If you are able to modify the code defining the enum, so you could add attributes to the values without modifying the actual enum values, then you could use this extension method.
/// <summary>
/// Retrieve the description of the enum, e.g.
/// [Description("Bright Pink")]
/// BrightPink = 2,
/// </summary>
/// <param name="value"></param>
/// <returns>The friendly description of the enum.</returns>
public static string GetDescription(this Enum value)
{
Type type = value.GetType();
MemberInfo[] memInfo = type.GetMember(value.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
{
return ((DescriptionAttribute)attrs[0]).Description;
}
}
return value.ToString();
}
If you have access to the Framework 3.5, you could do something like this:
Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>()
.Select(e=> new
{
Value = e,
Text = e.ToString().Replace("_", " ")
});
This will return you an IEnumerable of an anonymous type, that contains a Value property, that is the enumeration type itself, and a Text property, that will contain the string representation of the enumerator with the underscores replaced with space.
The purpose of the Value property is that you can know exactly which enumerator was chosen in the combo, without having to get the underscores back and parse the string.
Fill the combobox manually and do a string replace on the enum.
Here is exactly what you need to do:
comboBox1.Items.Clear();
MyEnum[] e = (MyEnum[])(Enum.GetValues(typeof(MyEnum)));
for (int i = 0; i < e.Length; i++)
{
comboBox1.Items.Add(e[i].ToString().Replace("_", " "));
}
To set the selected item of the combobox do the following:
comboBox1.SelectedItem = MyEnum.My_Value_2.ToString().Replace("_", " ");