【问题标题】:How to extract key from datasource of combobox如何从组合框的数据源中提取密钥
【发布时间】: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


    【解决方案1】:

    ComboBox.Items.Contains(object value) 将用于检查,因为键成为了组合框中所有项目的值。使用如下代码

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

    【讨论】:

    • comboBox1.Items.Contains(x) 不起作用,但我找到了 List keyValues = new List(); foreach (KeyValuePair cii in comboBox1.Items) { keyValues.Add(cii.Key); } for (int i = 0; i
    • 为我工作我测试了这段代码然后我回答了。
    • X 必须是 Item 的值,它在您的意义上是已知的关键。包含确定存在与否。
    • 您也可以避免在第一个循环中使用 cii.Key 检查 x 并设置 s=true 如果匹配
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 2012-07-13
    • 2020-11-28
    • 2019-11-21
    • 1970-01-01
    相关资源
    最近更新 更多