views:

548

answers:

3

How do I prevent code from executing in the SelectedValueChange event of a combobox when the combobox is being loaded for the first time? I need the code to execute when the user has selected a new value.

+2  A: 

You have a few options.

  1. Rather than letting the designer hookup the event handler you can do it yourself after loading

  2. You can set a flag in your application for "loading" and put code inside your SelectedValueChange event to check !loading before continuing.

Personally I would just add the handler after load if it is causing issues.

Mitchel Sellers
I just removed the handler and then hooked it up after loading
scantwell71
+5  A: 

If you only want some code to be executed when the user changes the combobox you can subscribe to SelectionChangeCommitted instead of SelectionChanged. "SelectionChangeCommitted " doesn't get called when the combobox selection is changed programmatically.

Chris Bennet
I know this an old post, but this tip helped me quite a lot!! thank you!
Roel
+1 this should be the accepted answer
dotjoe
A: 

I prefer the SelectedIndexChanged event as its behaviour seems to better model that of the underlying Win32 API making it predictable.

Some of the more esoteric events offered by the controls simply do not model the behaviour that one would expect from their name.