views:

774

answers:

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!

A: 

SelectedIndexChanged is typically what I use when doing something server side. Can you post some code?

peacedog
He's using WinForms.
SLaks
Yes, sorry I forgot to mention it's a standalone app.
John at CashCommons
+4  A: 

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

SLaks
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 at CashCommons
Remove the `Handles` clause from the definition of the handler method.
SLaks
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
This did the trick. Thanks!
John at CashCommons
+1  A: 

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
Now that you mention it, I think I actually did that on some other forms! Thanks for the reminder.
John at CashCommons
A: 

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.

Joe