Below is code where I am trying to extract key from combobox. If you look in to foreach (int value in comboBox1.ValueMember)
, I want to compare value 90 with all keys added in datasource of combobox. How to do that?
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";
}
}
}