1.使用KeyValuePair
//引用命名空间System.Collections.Generic
comboBox1.Items.Add(new KeyValuePair<string, string>("aaa", "1"));
comboBox1.Items.Add(new KeyValuePair<string, string>("bbb", "2"));
comboBox1.Items.Add(new KeyValuePair<string, string>("ccc", "3"));
comboBox1.DisplayMember = "key";
comboBox1.ValueMember = "value";
//获取值
MessageBox.Show(((KeyValuePair<string, string>)comboBox1.SelectedItem).Key + "," + ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value);2.自定义类MyListItem
MyListItem类:
public class MyListItem
{
private string _text;
private string _value;
public MyListItem(string text, string value)
{
_text = text;
_value = value;
}
public string Text
{
get { return _text; }
}
public string Value
{
get { return _value; }
}
//必须
public override string ToString()
{
return _text;
}
}添加数据项并读取示例:
comboBox1.Items.Add(new MyListItem("aaa", "1"));
comboBox1.Items.Add(new MyListItem("bbb", "2"));
comboBox1.Items.Add(new MyListItem("ccc", "3"));
//获取值
MessageBox.Show(((MyListItem)comboBox1.SelectedItem).Text + "," + ((MyListItem)comboBox1.SelectedItem).Value);
评论(0)
暂无评论。