user2780362
user2780362

Reputation: 67

Using Enums in Combobox

I have an enum as follows:

enum Keys : uint
{
  Key1 = 0x01,
  Key2 = 0x02,
  Key3 = 0x05
}

I am trying to populate a combobox with these options using the following code:

cboKeys.DataSource = Enum.GetValues(typeof(Keys));

I am serializing the selected value to save to disk, the problem I'm having is selecting the value from the combobox when loading the form again. I feel like I've tried a bunch of different ways that I've found on online, but nothing works. I'm using this code to set the SelectedValue:

public void SetKey(Keys key)
{
  cboKeys.SelectedValue = key;
}

Any help here would be appreciated as I have no idea what else to try.

Upvotes: 1

Views: 2077

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125332

To select the an enum value in a ComboBox, use SelectedItem. For example:

comboBox1.DataSource = Enum.GetValues(typeof(DayOfWeek));
comboBox1.SelectedItem = DayOfWeek.Thursday;

Upvotes: 1

Related Questions