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

[851 byte] By [codefund.com] at [2007-12-16]
# 1
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.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
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?

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
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! :(

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
just tried it

when it set the datasource the value isnt -1 . it is 0

which is bad since selecting clicking the first item in the listbox would also give you a 0

so what did I miss?

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
well if it's 0, that means the first item is selected so then what's the problem? is the problem in one of those other methods you're calling in your code?
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...