您需要将其绑定到key\value对象的集合,并使用DisplayMember和ValueMember属性来设置显示/返回的内容.
下面是一个例子:
public class ComboItem
{
public string stringValue { get; set; }
public int indexValue { get; set; }
}
public void LoadCombo()
{
List<ComboItem> list = new List<ComboItem>();
// populate list...
// then bind list
myComboBox.DisplayMember = "stringValue";
myComboBox.ValueMember = "indexValue";
myComboBox.DataSource = list;
}
Run Code Online (Sandbox Code Playgroud)
然后
myComboBox.SelectedText // will return stringValue
myComboBox.SelectedValue // will return indexValue
myComboBox.SelectedItem // will return the ComboItem itself
myComboBox.SelectedIndex // will return the items index in the list
Run Code Online (Sandbox Code Playgroud)
或者你可以通过创建一个自定义组合项添加一个Tag属性(通常用于存储这样的东西)来存储索引,在这里读取如何执行此操作