user2398888
user2398888

Reputation: 51

remove item selected of first comboBox from second comboBox c# windows form application

I am having a problem:

I am making a Windows Forms application in C#.

The problem I'm having is I have 4 ComboBoxes, and when I select an item from comboBox1 that item should be removed from comboBox2, comboBox3 and comboBox4.

Similarly, the selected item from comboBox2 should be removed from comboBox3 and comboBox4, and so on.

I have tried this but can't get my head around it.

Upvotes: 2

Views: 1940

Answers (1)

hm1984ir
hm1984ir

Reputation: 554

You should code something like this in your comboBoxes selectedChange events:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    for (int i = 0; i < comboBox2.Items.Count; i++)
    {
        if (comboBox2.Items[i] == comboBox1.SelectedItem)
        {
            comboBox2.Items.Remove(comboBox2.Items[i]);
            i--;
        }
    }
}

it works if your items are string, if you have custom object you should cast items and then compare specific property on them like id for example.

Upvotes: 2

Related Questions