第一種 最簡潔的方法this
Dictionary<string, string> list = new Dictionary<string, string> { {"this is i1", "001"}, {"this is i2", "002"} };orm
private void Form1_Load(object sender, EventArgs e) {string
comboBox1.Items.AddRange(list.Keys.ToArray());io
}百度
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {object
string a = list[comboBox1.SelectedText];騰訊
textBox1.Text = a;方法
第二種 笨點的方法ember
綁定數據
private void Form1_Load(object sender, EventArgs e) {
var list = new Dictionary<string, string> { {"this is i1", "001"}, {"this is i2", "002"} };
comboBox1.Items.AddRange(new object[] {list.ToArray()});
comboBox1.DisplayMember = "key";
comboBox1.ValueMember = "value";
}
//綁定數據 以上方法失效用這個方法
Dictionary<string, string> dict = new Dictionary<string, string> {{"baidu.com", "百度"}, {"goolge.com", "谷歌"}, {"qq.com", "騰訊"}};
comboBox1.DataSource = new BindingSource(dict, null);
comboBox1.ValueMember = "Key";//文本對應的值
comboBox1.DisplayMember = "Value";//顯示的文本
獲取值
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
string a = ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value;
textBox1.Text = a;
}