4

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!

John
  • 15,990
  • 10
  • 70
  • 110
  • 1
    I couldn't reproduce your problem. I added a ComboBox and created a SelectedIndexChanged event in the designer. In Form_Load I added items (using comboBox1.Items.Add("xx")) and each time I added an item the SelectedIndexChanged did not fire. I wonder if your problem is caused by how you've set up your ComboBox on your form. – Jay Riggs Dec 24 '09 at 00:37
  • Jay, thanks for trying! I'll try stepping through the code some more to see if what I said was actually what was happening. – John Dec 24 '09 at 00:39
  • 2
    I was able to reproduce your problem with your code snippet. I like SLaks solution; it worked for me! – Jay Riggs Dec 24 '09 at 00:44
  • Jay, I added information about how I added the event handler for the combobox. I just double-clicked the combobox on the form. This must add an event handler automatically then (somewhere I didn't want it)? – John Dec 24 '09 at 00:56

3 Answers3

10

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.

Joe
  • 122,218
  • 32
  • 205
  • 338
  • 1
    +1 this is the only answer that actually answers the question instead of providing a workaround. Nothing wrong with the workarounds, but I prefer this. – Colin Pickard Dec 03 '10 at 14:43
5

You could only add the event handler after loading the data. (Using the AddHandler keyword)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • SLaks, I added information about how I added the event handler for the combobox (double-clicked the combobox on the form). I'll need to pull that (automatically-added) event handler out somehow to do what you suggest? – John Dec 24 '09 at 00:57
  • 2
    Remove the `Handles` clause from the definition of the handler method. – SLaks Dec 24 '09 at 01:00
  • 2
    One way to do this is in the designer select the ComboBox, go to the properties Window, click the Events button and find (and delete) the entry that's in the SelectedIndexChanged item. – Jay Riggs Dec 24 '09 at 01:04
1

You could have a boolean which you use to determine whether you are the one doing the changing. When you begin to change the items, set it to true. When you are finished, set it to false. In the event handler, you can test the boolean to determine if the user is the initiator of the changes and ignore the event if not.

Zach Johnson
  • 23,678
  • 6
  • 69
  • 86
  • Now that you mention it, I think I actually did that on some other forms! Thanks for the reminder. – John Dec 24 '09 at 00:48