Setting a datasource for a listbox causes a SelectedIndexChanged. NO!!!!
I have a DB winapp using one form and a bunch of listboxes
I re-assign the datasources of the listboxes as other listboxes are changed
this causes the SelectedIndexChanged to occur. I do not want this. I want that event to only happen when the USER changes something. What can I do? As of now I have a bunch of boolean variables like this
Private Sub lbZones_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbZones.SelectedIndexChanged
If ZoneIndexChanged Then
ScenarioIndexChanged = False
CrossingIndexChanged = False
scenarioCrossingsIndexChanged = False
crossings_bind()
scenario_bind()
scenarioCrossings_Bind()
CrossingDetails_bind()
ScenarioIndexChanged = True
CrossingIndexChanged = True
scenarioCrossingsIndexChanged = True
End If
End Sub
You'll either have to dynamically add the handler (take off the Handles clause on the Sub and then use AddHandler and RemoveHandler for when you want it to handle the event) or put an if statement in your SelectedIndexChanged, because more than likely, you're blowing an error because when you set the DataSource again, the SelectedIndex will be -1, so just check to make sure you don't execute your code, unless the SelectedIndex is greater than -1.
is the reaosn no one else has this problem because I am giong about it the wrong way
Should I only be setting the datasource once? but then how do I refresh the data if I use objects that do the SQL and send me back a datatable?
I don't think there's such thing as a "wrong" way, it's just that there might be a "better" way, but I'm not sure offhand.
I think if you just put an If lbZones.SelectedIndex > -1 Then -- End If around your current code, you'll be fine. Did you try that?
On a side note, I've always wondered why the ListBox doesn't have an Event like the ComboBox does. It's called SelectionChangeCommitted. It's great, because it only gets fired when the actual user clicks and makes a change, but alas, the ListBox doesn't have it! :(