If you need a value and a label (label), create the appropriate class
class ComboItem { public int ID { get; set; } public string Text { get; set; } }
In ComboBox, you set the DisplayMember property to Text and the ValueMember ID property.
DropDownStyle ComboBox defines its behavior. DropDownStyle.DropDown allows the user to enter text. Using DropDownStyle.DropDownList user can only select items from a list.
You can populate the ComboBox as follows:
myCombo.DataSource = new ComboItem[] { new ComboItem{ ID = 1, Text = "One" }, new ComboItem{ ID = 2, Text = "Two" }, new ComboItem{ ID = 3, Text = "Three" } };
DataSource can be any enumerated.
You can get the selected identifier, for example,
int id = (int)myComboBox.SelectedValue;
Please note that you can add any type of item to the ComboBox. If you do not specify the DisplayMember and ValueMember , the ComboBox uses the object's ToString method to determine the displayed text, and you can get the selected item (not the selected value) through the SelectedItem property.
If you add objects of this type ...
class Person { public int PersonID { get; set } public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() { return FirstName + " " + LastName; } }
... in ComboBox, you can get the selected item like this
Person selectedPerson = (Person)myComboBox.SelectedItem; int personID = selectedPerson.PersonID;
ComboBox will display the names and surnames of individuals.