If you are using .NET 3.5, you can 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 simply do:
MyEnum val = MyEnum.My_Value_1; comboBox1.DataSource = val.GetFriendlyNames(); comboBox1.SelectedItem = val.GetFriendlyName();
This should work with any Enum. You must ensure that you have a using statement for the namespace that includes the EnumExtensions class.
Luke Sampson Jul 09 '09 at 6:52 2009-07-09 06:52
source share