Leinad Leinad - 2 months ago 18
C# Question

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.

Is this possible?

Example

Answer

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;

 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,

Comments