public Form1()
{
InitializeComponent();
List<KeyValuePair<string, int>> list = new List<KeyValuePair<string, int>>();
list.Add(new KeyValuePair<string, int>("apple", 1));
list.Add(new KeyValuePair<string, int>("mango", 2));
list.Add(new KeyValuePair<string, int>("orange", 3));
comboBox1.DisplayMember = "Key";
comboBox1.ValueMember = "Value";
comboBox1.DataSource = list;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedValue = (int)comboBox1.SelectedValue;
}
Or you can use your custom class to bind as combobox data source
public class ComboItem
{
public string stringValue { get; set; }
public int indexValue { get; set; }
}
public void LoadCombo()
{
List<ComboItem> list = new List<ComboItem>();
myComboBox.DisplayMember = "stringValue";
myComboBox.ValueMember = "indexValue";
myComboBox.DataSource = list;
}
Then
myComboBox.SelectedText
myComboBox.SelectedValue
myComboBox.SelectedItem
myComboBox.SelectedIndex
http://stackoverflow.com/questions/4691731/winforms-c-adding-items-to-combox-and-controlling-the-item-value-numeric[
^]