Display enumeration in ComboBox with spaces

I have an enumeration, for example:

enum MyEnum { My_Value_1, My_Value_2 } 

Via:

 comboBox1.DataSource = Enum.GetValues(typeof(MyEnum)); 

But now my question is: how to replace "_" with "" so that it becomes elements with spaces instead of underscores? And that the data binding object still works

+15
c #
Jul 09 '09 at 5:45
source share
7 answers

If you have access to Framework 3.5, you can 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 the Value property, which is the enumeration type itself, and the Text property, which will contain the string representation of the enumerator with underscores replaced by a space.

The purpose of the Value property is that you can know exactly which enumerator was selected in the combo, without having to return underscores and parse the string.

+15
Jul 09 '09 at 6:09
source share

If you can change the code that defines an enumeration so that you can add attributes to values ​​without changing the actual values ​​of the enumeration, then you can use this extension method.

 /// <summary> /// Retrieve the description of the enum, eg /// [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(); } 
+5
Jul 09 '09 at 7:10
source share

Fill in the combo box manually and replace the string with an enumeration.

Here is 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 in the combo box, do the following:

 comboBox1.SelectedItem = MyEnum.My_Value_2.ToString().Replace("_", " "); 
+3
Jul 09 '09 at 5:49
source share

Try it...

 comboBox1.DataSource = Enum.GetValues(typeof(MyEnum)) .Cast<MyEnum>() .Select(e => new { Value = e, Description = e.ToString().Replace("_"," ") }) .ToList(); comboBox1.DisplayMember = "Description"; comboBox1.ValueMember = "Value"; 

... though, I would be more likely to use the Description attribute (according to Steve Kren's answer).

+3
Jun 30 '11 at 2:55 a.m.
source share

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.

0
Jul 09 '09 at 6:52
source share

I think it is not a good idea to map the internal name enum in user space. What if you reorganize your transfer amount? Therefore, I suggest you take a look at the article (Localization of .NET Enums) . Using the technique described in this article, you can not only replace "_" with spaces, but also make a different enumeration representation for different languages ​​(using resources).

0
Jul 09 '09 at 8:31
source share

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 at a lower cost; 'e' in event handlers tends to be an argument to EventArgs . Regarding the LINQ and IEnumerable approach, it seems harder and harder to adapt for non-WPF ComboBox es with .NET 3.5

0
Apr 04 2018-12-12T00:
source share



All Articles