[RESOLVED] [2005] Problem with ComboBox_SelectedIndexChanged event
I have ComboBox bound to DataTable (Lookup table) and I have some code in ComboBox_SelectedIndexChanged event. I want that code to be executed when user select value from ComboBox.
Problem is that ComboBox_SelectedIndexChanged event fired on form load which I dont want, I expect it to be fired only when user pick item in combobox.
How to avoid ComboBox_SelectedIndexChanged event to be fired on form load?
Re: [2005] Problem with ComboBox_SelectedIndexChanged event
Quote:
Originally Posted by dranko
How to avoid ComboBox_SelectedIndexChanged event to be fired on form load?
You can't. The event is raised when the SelectedIndex property changes. Think outside the square a bit. In the event handler, test whether the Load event handler has already been executed. You would do that by setting a boolean flag at the end of the Load event handler.
Re: [2005] Problem with ComboBox_SelectedIndexChanged event
Yes, that works.
My problem is in my thinking that SelectedIndex changed only when I click on ComboBox and pick item, but obviously I was wrong.
Thanks
Re: [2005] Problem with ComboBox_SelectedIndexChanged event
When you set the DataSource property of the ComboBox it automatically selects the first item. If you don't want the first item selected you have to explicitly set the SelectedIndex to -1, but that means two SelectedIndexChanged events rather than none.
Re: [2005] Problem with ComboBox_SelectedIndexChanged event
I like your answers because you give explanation how something works (not only solution), and I learned a lot from you.
Thanks.
Re: [RESOLVED] [2005] Problem with ComboBox_SelectedIndexChanged event
I was having this same issue. You want to use the SelectionChangeCommitted event as that will only respond to the user change on the control, not a program change of a index.
Re: [RESOLVED] [2005] Problem with ComboBox_SelectedIndexChanged event
Eh, why didn't I read help more carefully.
"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."
Thanks.
Re: [RESOLVED] [2005] Problem with ComboBox_SelectedIndexChanged event
New information for me too. :thumb: