简体   繁体   English

如何从组合框的数据源中提取密钥

[英]How to extract key from datasource of combobox

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. 如果您查看foreach (int value in comboBox1.ValueMember) ,我想将值90与在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";            
        }   
    }
}

ComboBox.Items.Contains(object value) will be used to check because keys became values of all items in comboBox.Use like Following code ComboBox.Items.Contains(object value)将用于检查,因为键成为comboBox中所有项的值。

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();
        }
    }  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM