I want to use a C# Windows Forms combo box to select the value of an enum:
this.comboBoxColor.DataSource = System.Enum.GetValues(typeof(System.Drawing.KnownColor));
But when I put this in the InitializeComponent
, it replaces this line with a static assignment of an array with all items in the enum. It does this twice, once for the Datasource
, and once for the Items
property.
But these things don't work together. When having a bound DataSource
, adding items to the Items
list causes an error and when doin it the other way around assigning the SelectedValue
property doesn't work any more.
I've tried using a separate method to do this outside the InitializeComponent
method. And simply setting the DataSource as above in the separate method produces the following error:
System.InvalidOperationException: 'Can't set the SelectedValue in a ListControl with an empty ValueMember.'
Edit: Microsoft says using a simple array as a data source should be possible: https://msdn.microsoft.com/nl-nl/library/x8160f6f(v=vs.110).aspx
It's possible to specify the data source in the designer, but there it only allows selecting classes. What must a class implement to make that work?