Binding an enum to a WinForms combo box, and then setting it

前端 未结 28 2164
心在旅途
心在旅途 2020-11-28 04:33

a lot of people have answered the question of how to bind an enum to a combo box in WinForms. Its like this:

comboBox1.DataSource = Enum.GetValues(typeof(MyE         


        
28条回答
  •  星月不相逢
    2020-11-28 04:57

    The code

    comboBox1.SelectedItem = MyEnum.Something;
    

    is ok, the problem must reside in the DataBinding. DataBinding assignments occur after the constructor, mainly the first time the combobox is shown. Try to set the value in the Load event. For example, add this code:

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        comboBox1.SelectedItem = MyEnum.Something;
    }
    

    And check if it works.

提交回复
热议问题