scantwell71
scantwell71

Reputation: 113

Winforms ComboBox SelectedValueChange event

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.

Upvotes: 4

Views: 1941

Answers (3)

user21826
user21826

Reputation: 3644

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.

Upvotes: 0

Chris Bennet
Chris Bennet

Reputation: 617

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.

Upvotes: 10

Mitchel Sellers
Mitchel Sellers

Reputation: 63126

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.

Upvotes: 3

Related Questions