Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

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

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

MyValues is the enum type

[C#]

// Setup the binding as follows:
comboBox1.DataSource = Enum.GetValues(typeof MyValues);

// SelectedValueChanged event for the ComboBox.
private void ComboBox1ValueChanged(object sender, EventArgs e)
{
  MyValues v = (MyValues) comboBox1.SelectedValue;
}

[Visual Basic]

comboBox1.DataSource = Enum.GetValues(Type.GetType(MyValues))

Private Sub ComboBox1ValueChanged(ByVal sender As Object, _
    ByVal e As EventArgs)
  Dim v As MyValues = CType(comboBox1.SelectedValue, MyValues)
End Sub

Jay Harlow



Page view counter