简体   繁体   English

如何启用和禁用2组合框中的值。 C#

[英]How enable and disable values from 2 combobox. C#

I have 2 comboboxes with same values, my question is, if I selected item 1 from "combobox 1", on "combobox 2", item 1 should be disabled or hide, and viceversa. 我有2个具有相同值的组合框,我的问题是,如果我从“ combobox 1”中的“ combobox 2”中选择了项1,则应该禁用或隐藏项1,反之亦然。

Is this possible? 这可能吗?

Example

I don't know how you populate your combobox but here is a sample for you, 我不知道您如何填充组合框,但是这里有一个示例供您选择,

List<string> items = new List<string>();
        private void Form1_Load(object sender, EventArgs e)
        {
            items.Add("test");
            items.Add("asd");
            items.Add("qwe");
            comboBox1.DataSource = items;
            comboBox2.DataSource = items;
        }

On Combobox1's selectedIndexChanged event; 在Combobox1的selectedIndexChanged事件上;

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<string> values = (List<string>)comboBox2.DataSource;
            values = items.Where(x => x != comboBox1.SelectedItem.ToString()).ToList();
            comboBox2.DataSource = values;
        }

Hope helps, 希望有帮助,

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

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