【发布时间】:2018-07-19 15:22:31
【问题描述】:
下面是我试图从组合框中提取密钥的代码。如果您查看foreach (int value in comboBox1.ValueMember),我想将值 90 与组合框数据源中添加的所有键进行比较。该怎么做?
namespace SetComboBoxEnum
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
AddComboBoxItems(comboBox1, 10, "Sunday");
AddComboBoxItems(comboBox1, 20, "Tuesday");
AddComboBoxItems(comboBox1, 100, "Friday");
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = comboBox1.SelectedValue.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
bool s=true;
int x = 90;
foreach (int value in comboBox1.ValueMember)
{
if (x == value)
{
s = true;
}
}
if (s == true)
{ comboBox1.SelectedValue = x; }
else
{
comboBox1.Text = x.ToString();
}
}
IDictionary<int, string> comboSource = new Dictionary<int, string>();
public void AddComboBoxItems(ComboBox cmbbox, int itemvalue, string itemstring)
{
comboSource.Add(new KeyValuePair<int, string>(itemvalue, itemstring));
cmbbox.DataSource = new BindingSource(comboSource, null);
cmbbox.DisplayMember = "Value";
cmbbox.ValueMember = "Key";
}
}
}
【问题讨论】:
标签: c# combobox keyvaluepair