The SelectedIndexChanged
event gets fired in my application from a combo box when:
- the user chooses a different item in the combo box, or when:
- my own code updates the combo
box's
SelectedItem
to reflect that the combo box is now displaying properties for a different object.
I am interested in the SelectedIndexChanged
event for case 1, so that I can update the current object's properties. But in case 2, I do not want the event to fire, because the object's properties have not changed.
An example may help. Let's consider that I have a list box containing a list of people and I have a combo box representing the nationality of the currently selected person in the list. Case 1 could happen if Fred is currently selected in the list, and I use the combo box to change his nationality from English to Welsh. Case 2 could happen if I then select Bob, who is Scottish, in the list. Here, my list update event-handler code sees that Bob is now selected, and updates the combo box so that Scottish is now the selected item. This causes the combo box's SelectedIndexChanged
event to be fired to set Bob's nationality to Scottish, even though it already is Scottish.
How can I update my combo box's SelectedItem
property without causing the SelectedIndexChanged
event to fire? One way would be to unregister the event handler, set SelectedItem
, then re-register the event handler, but this seems tedious and error prone. There must be a better way.