1

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";            
        }   
    }
}
R15
  • 13,982
  • 14
  • 97
  • 173
DipakM
  • 37
  • 6

1 Answers1

0

ComboBox.Items.Contains(object value) will be used to check because keys became values of all items in comboBox.Use like Following code

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        AddComboBoxItems(comboBox1, 10, "Sunday");
        AddComboBoxItems(comboBox1, 20, "Tuesday");
        AddComboBoxItems(comboBox1, 100, "Friday");
    }

    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";
    }

    private void button2_Click(object sender, EventArgs e)
    {
        int x = 20;
        if (comboBox1.Items.Contains(x))
        { comboBox1.SelectedValue = x; }
        else
        {
            comboBox1.Text = x.ToString();
        }
    }  
}
Arslan Ali
  • 450
  • 4
  • 12
  • comboBox1.Items.Contains(x) is not working but, I found way List keyValues = new List(); foreach (KeyValuePair cii in comboBox1.Items) { keyValues.Add(cii.Key); } for (int i = 0; i < keyValues.Count; i++) { if (x == keyValues[i]) { s = true; } } – DipakM Jul 19 '18 at 06:28
  • Working for me I tested this code then I did answer. – Arslan Ali Jul 19 '18 at 06:58
  • X must be value for Item which is known to be key in your sense.Contains determine that exist or not. – Arslan Ali Jul 19 '18 at 07:01
  • You can also avoid to fill keyValues as such check x with cii.Key in first loop and set s=true if match – Arslan Ali Jul 19 '18 at 07:04