Search code examples
c#selectedindex

Comparing a Selected Index Combo Box to another Selected Index Combo Box


Basically what I am trying to do, is compare the selected Index of two combo boxes in Win Forms. If ComboBoxA.SelectedIndex == 1 and, ComboBoxB.SelectedIndex == 1, I need to prevent that from happening, by changing ComboBoxB.SelectedIndex to 2.

I tried doing this through an If statement and couldn't get the results I was looking for.

  if (Northern.SelectedIndex == 1 && NorthernEnd.SelectedIndex == 1)
                NorthernEnd.SelectedIndex = 2;

However, I can't seem to get this to work. I am a beginner in C# working on my last assignment of the term.


Solution

  • You can handle the event to prevent the action:

    if (Northern.SelectedIndex == 1 && NorthernEnd.SelectedIndex == 1)
        e.Handled = true;
    

    Here is the MSDN ref.