How to map enums to comboboxes in C#.

  • Download Gist
CSharp_Winforms_MappingEnumsToComboboxes_ActualMapping.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
public partial class MappingEnumToComboboxesForm : Form
{
public MappingEnumToComboboxesForm()
{
InitializeComponent();
//Setting the enums as the data sources of the comboboxes
comboBoxNormal.DataSource =
Enum.GetValues(typeof(WdtOscillatorCycles));
comboBoxImproved.DataSource =
Enum.GetValues(typeof(WdtOscillatorCyclesImproved));
}
 
public WdtOscillatorCyclesImproved WdtOscillatorCyclesOption
{
get
{
//The result must be cast to the enum's type
return (WdtOscillatorCyclesImproved)(comboBoxImproved.SelectedItem);
}
set
{
//You don't need a cast here.
comboBoxImproved.SelectedItem = value;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.