user4214050
user4214050

Reputation:

Deselect combobox selected value

First of all, I apologize to the community because the title may seem a bit 'misleading from what actually is the real problem. I have made a code that populates two combobox with the same values, with each change that occurs on the combobox I perform a check to see if the selected value is equal to another combobox; if it is equal, then execute a particular code otherwise proceed. I need to implement a de-selection of value and I did it with:

ComboBox1.SelectedIndex = -1 
Combobox2.SelectedIndex = -1

This works fine, but the code that checks if the values are equal interfere with this operation. In fact, within each combobox I have something like this:

Private Sub ComboBox2_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged

If ComboBox3.SelectedIndex = ComboBox2.SelectedIndex Then
MsgBox ("You can not compare two equal teams", "Warning")
off ()
End If
...

where "off ()" is the function that then doesn't continue what you're doing. How do I solve this problem?

Upvotes: 1

Views: 5015

Answers (1)

the_lotus
the_lotus

Reputation: 12748

You'll have to disable the event when resetting the combobox. This can be done by removing the event with RemoveHandler/AddHandler and adding it again.

An other option is to use a flag. (This is just an example to show an idea, the flag variable should be properly placed).

Private FreezeEventFlag As Boolean = False ' or True, it depends..
' Declare it toplevel, initialize its value depending on
' the way you're going to initialize your Comboboxes selected values.
' Then set it's value to True each time you want to
' disable the event handling in any code block that resets
' SelectedIndexes to -1 like below :

    ' ...
    FreezeEventFlag = True
    ComboBox1.SelectedIndex = -1 
    Combobox2.SelectedIndex = -1
    FreezeEventFlag = False ' Don't forget to set the Flag to false !

Private Sub ComboBox2_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged

    If FreezeEventFlag Then Exit Sub ' Just ignore this event this time.

    If ComboBox3.SelectedIndex = ComboBox2.SelectedIndex Then
        MsgBox ("You can not compare two equal teams", "Warning")
        off ()
    End If
End Sub

Upvotes: 1

Related Questions