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

前端 未结 28 2160
心在旅途
心在旅途 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 04:55

    That was always a problem. if you have a Sorted Enum, like from 0 to ...

    public enum Test
          one
          Two
          Three
     End
    

    you can bind names to combobox and instead of using .SelectedValue property use .SelectedIndex

       Combobox.DataSource = System.Enum.GetNames(GetType(test))
    

    and the

    Dim x as byte = 0
    Combobox.Selectedindex=x
    

提交回复
热议问题