How do I bind the values of an enum to a ComboBox?

Platform: WinForms| Category: ComboBox

This entry was created using the feedback provided by Jay Harlow in the newsgroups.

The enum values can be bound to a combobox as follows:


[C#]
// Setup the binding as follows:
// MyValues is the enum type
comboBox1.DataSource = Enum.GetValues(typeof MyValues);
[VB]
comboBox1.DataSource = Enum.GetValues(Type.GetType(MyValues))

Then in the SelectedValueChanged event for the ComboBox.


[C#]
private void ComboBox1ValueChanged(object sender, EventArgs e)
{
    MyValues v = (MyValues)this.comboBox1.SelectedValue;
}
[VB]
Private  Sub ComboBox1ValueChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim v As MyValues = CType(Me.comboBox1.SelectedValue, MyValues)
End Sub

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.