combobox selectedindexchanged fires when filled with dataset

Ultrawhack

Well-known member
Joined
Jul 5, 2006
Messages
164
Location
Canada
Programming Experience
3-5
I fill my unbound combobox using a dataset on form load.
When the form loads it automatically fires the code in the combobox selectedindexchanged event without the user changing the selection.
Obviously it is recognising the fill as part of the event.

How can I get this code to fire only when combobox is changed manually ?

Thanks
 
The SelectedIndexChanged event is raised whenever the SelectedIndex changes. When you bind data to a ComboBox the first item is selected, thus changing the SelectedIndex from -1 to 0. That's a fact of life. The event is going to be raised when you bind the data.

Now, that means that in SelectedIndexChanged event handler you need to test whether this is as a result of the data being bound or as a result of a user selection. This will require an If statement. An If statement tests a boolean expression. You need a boolean expression to indicate whether data is in the process of being bound or not.

The logical (excuse the pun) thing to do is declare a class-level Boolean variable that will be one value while data is being bound and the other when it's not. When does this data get bound? Is it while the form is loading? If so then you set the variable to one value when the form is created and then toggle the value in the Shown event handler, which is executed when the form is first displayed. If the data is bound at some other time then it's up to you to toggle the variable before you bind the data and then back again afterwards.
 
Back
Top