从ComboBox中获取价值
本文关键字:获取 ComboBox | 更新日期: 2023-09-27 18:03:18
这是我的代码
string key = ((KeyValuePair<string, string>)ComboBox1.SelectedItem).Key;
string value = ((KeyValuePair<string, string>)ComboBox1.SelectedItem).Value;
绑定到组合框:
var dict = new Dictionary<string, string>();
for(int i=1; i<=10; i++)
{
dict.Add((i).ToString(), String.Format("Item {0}", i));
}
ComboBox.DataSource = new BindingSource(dict, null);
ComboBox.ValueMember = "Key";
ComboBox.DisplayMember = "Value";
得到价值:
KeyValuePair<string, string> kvp = (KeyValuePair<string, string>)ComboBox.SelectedItem;
foreach(KeyValuePair k in kvp)
{
Console.WriteLine("Key: {0}, Value: {1}", k.Key, k.Value);
}