-1

There is an enum in my project like this:

public enum UserFrienlyEnum
{
    [Description("it need spec training")]
    SPECIAL_TRAINING = 1,
    [Description("it need normal training")]
    NORMAL_TRAINING = 2,
    [Description("it need simple training")]
    SIMPLE_TRAINING = 3
}

I bound this enum to a combobox by using this method:

public static void setEnumValues(ComboBox cxbx, Type typ)
{
    if (!typ.IsEnum)
    {
        throw new ArgumentException("Only Enum types can be set");
    }

    List<KeyValuePair<string, int>> list = new List<KeyValuePair<string, int>>();

    foreach (int i in Enum.GetValues(typ))
    {
        string name = Enum.GetName(typ, i);
        string desc = name;
        FieldInfo fi = typ.GetField(name);

        // Get description for enum element
        DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

        if (attributes.Length > 0)
        {
            string s = attributes[0].Description;

            if (!string.IsNullOrEmpty(s))
            {
                desc = s;
            }
        }

        list.Add(new KeyValuePair<string, int>(desc, i));
    }
    // NOTE: It is very important that DisplayMember and ValueMember are set before DataSource.
    //       If you do, this works fine, and the SelectedValue of the ComboBox will be an int
    //       version of the Enum.
    //       If you don't, it will be a KeyValuePair.
    cxbx.DisplayMember = "Key";
    cxbx.ValueMember = "Value";
    cxbx.DataSource = list;
}

And use above method for binding combobox to myEnum in this way:

setEnumValues(comboBox, typeof(myEnum));

Now question is how I can set my combobox item or value to a specific one, something like this:

combobox.SelectedValue = myEnum.value;

My project is a C# windows project in Visual Studio 2010 environment.

Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44

2 Answers2

1

I replicated your code, I assume this is what you have, and this is working perfectly fine for me.

namespace WindowsFormsApplication1
{
    public class MyClass
    {
        public string Something { get; set; }
        public UserFrienlyEnum foo { get; set; }
    }
    public enum UserFrienlyEnum
    {
        [Description("it need spec training")]
        SPECIAL_TRAINING = 1,
        [Description("it need normal training")]
        NORMAL_TRAINING = 2,
        [Description("it need simple training")]
        SIMPLE_TRAINING = 3
    }
    public partial class Form1 : Form
    {


        private void Form1_Load(object sender, EventArgs e)
        {
            setEnumValues(this.comboBox1, typeof(UserFrienlyEnum));
            MyClass variable = new MyClass();
            variable.foo = UserFrienlyEnum.NORMAL_TRAINING;
            this.comboBox1.SelectedValue = (int)variable.foo;

        }

        public static void setEnumValues(ComboBox cxbx, Type typ)
        {
            if (!typ.IsEnum)
            {
                throw new ArgumentException("Only Enum types can be set");
            }

            List<KeyValuePair<string, int>> list = new List<KeyValuePair<string, int>>();

            foreach (int i in Enum.GetValues(typ))
            {
                string name = Enum.GetName(typ, i);
                string desc = name;
                FieldInfo fi = typ.GetField(name);

                // Get description for enum element
                DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attributes.Length > 0)
                {
                    string s = attributes[0].Description;
                    if (!string.IsNullOrEmpty(s))
                    {
                        desc = s;
                    }
                }

                list.Add(new KeyValuePair<string, int>(desc, i));
            }
            // NOTE: It is very important that DisplayMember and ValueMember are set before DataSource.
            //       If you do, this works fine, and the SelectedValue of the ComboBox will be an int
            //       version of the Enum.
            //       If you don't, it will be a KeyValuePair.
            cxbx.DisplayMember = "Key";
            cxbx.ValueMember = "Value";
            cxbx.DataSource = list;
        }

        public Form1()
        {
            InitializeComponent();
        }


    }
}

Result:

enter image description here

Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75
0

Thanks to my friend Hanlet this is the simplest answer:

combobox.SelectedValue = (int) myEnum.value;

casting it to int was the key.

Hossein Hadavi
  • 251
  • 2
  • 3
  • 13
  • Hmm, I bound the DataSource of my combobox to the enum. Then I set `combobox.SelectedItem` to the desired enum value. – Gaʀʀʏ Nov 09 '13 at 22:33