I'm developing in VB.NET with Visual Studio 2005.
I have a ComboBox (myCombo) on a form that gets populated within the Load method.
I also have handled myCombo.SelectedIndexChanged to change a label on the form.
Edit: The way I added the event handler was by double-clicking on the combo box on the designer. Skeleton code then came up in the code view.
It looks like what's happening is when the form loads, SelectedIndexChanged gets fired each time an item is added to myCombo.
This isn't what I want, but I'm thinking there's another event handler that only gets called when the user changes the selection.
Here's some code for what I have:
Private Sub myDlg_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' this is all I do with myCombo in this sub
list = GetListOfItemsToAdd()
myCombo.DataSource = list
myCombo.DisplayMember = "DisplayMember"
myCombo.ValueMember = "ValueMember"
End Sub
Could someone point me in the right direction?
Thanks as always.
Update: The solution I used was to remove the Handles clause after the event generator, and add this before the "End Sub" above:
AddHandler myCombo.SelectedIndexChanged, AddressOf myCombo_SelectedIndexChanged
Thanks everyone!
SelectionChangeCommitted is the event handler that's called when the user changes the ComboBox selection.
From the MSDN documentation for SelectionChangeCommitted:
SelectionChangeCommitted is raised only when the user changes the combo box selection. Do not use SelectedIndexChanged or SelectedValueChanged to capture user changes, because those events are also raised when the selection changes programmatically.
However do note that there is a bug which means that in some circumstances the SelectionChangeCommitted event is not fired (specifically: use the keyboard to drop down the list, scroll to a new item then tab to a different control. The selection is changed but the SelectionChangeCommitted event isn't fired). See http://connect.microsoft.com/VisualStudio/feedback/details/115189/selectionchangecommitted-event-sometimes-not-raised-by-combobox
In practice I've found that this is not perceived as a problem by users - so I've continued to use SelectionChangeCommitted rather than other workarounds that use SelectedIndexChanged. But YMMV of course.
You could only add the event handler after loading the data. (Using the AddHandler
keyword)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With