Display enum in ComboBox with spaces

后端 未结 7 1128
北海茫月
北海茫月 2020-12-31 06:03

I have an enum, example:

enum MyEnum
{
My_Value_1,
My_Value_2
}

With :

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


        
7条回答
  •  感动是毒
    2020-12-31 07:02

    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("_", " ");
    

提交回复
热议问题