CombobBox SelectedIndexChanged Event
-
I had posted this question two weeks back but till date no one has responed. Is this indeed an issue with the combobox? I hope I'm clear with my question and this time I will get some help from this forum. ComboBox control has got selectedindexchanged event. As the name suggest this event should fire when selected item change for the combobox. But this event fires even when the currently selected item is selected again from the dropdown list of combobox. e.g. if I have loaded following items in my combobox say item1 item2 item3 item4 with item1 as selected when my application starts. Now if from UI, I again select the "item1" (which is already selected in combo box) then also the selectedindexchanged event fires. In this case the index has not changed still the selectedindexchanged event fires. I find this bit strange. Is there anyway to findout when the selected item has changed by User. I want some kind of notification when user indeed changes the combobox selected item (not when combobox.selecteditem is set through code)? regards KC
-
I had posted this question two weeks back but till date no one has responed. Is this indeed an issue with the combobox? I hope I'm clear with my question and this time I will get some help from this forum. ComboBox control has got selectedindexchanged event. As the name suggest this event should fire when selected item change for the combobox. But this event fires even when the currently selected item is selected again from the dropdown list of combobox. e.g. if I have loaded following items in my combobox say item1 item2 item3 item4 with item1 as selected when my application starts. Now if from UI, I again select the "item1" (which is already selected in combo box) then also the selectedindexchanged event fires. In this case the index has not changed still the selectedindexchanged event fires. I find this bit strange. Is there anyway to findout when the selected item has changed by User. I want some kind of notification when user indeed changes the combobox selected item (not when combobox.selecteditem is set through code)? regards KC
You have to store your selected index, and compare it to what is in the control after the event fires. The event should check first, then store the value for next time.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
You have to store your selected index, and compare it to what is in the control after the event fires. The event should check first, then store the value for next time.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Christian Thanks for your reply. I agree to your solution where I have to remember the previously selected index and match it with the newly selected index in the eventhandler. If new and previous indexes are same then skip the execution of handler. But this is not going to serve my purpose. What I want is, I want to skip the execution of ComboboxSelectedIndexChanged Eventhandler when the already selected item in the combobox is again selected by user (i.e. user clicks the combobox and select the same item again). I don't mind if combobox SelectedIndex changed event fires through some code i.e. ComboBox.SelectedIndex = 2; So in nutshell want to restrict the eventhandler when same item is selected through user interaction.
-
Christian Thanks for your reply. I agree to your solution where I have to remember the previously selected index and match it with the newly selected index in the eventhandler. If new and previous indexes are same then skip the execution of handler. But this is not going to serve my purpose. What I want is, I want to skip the execution of ComboboxSelectedIndexChanged Eventhandler when the already selected item in the combobox is again selected by user (i.e. user clicks the combobox and select the same item again). I don't mind if combobox SelectedIndex changed event fires through some code i.e. ComboBox.SelectedIndex = 2; So in nutshell want to restrict the eventhandler when same item is selected through user interaction.
Well, you plainly can't stop the event from firing. Until it fires, you can't make your code run at all.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
I had posted this question two weeks back but till date no one has responed. Is this indeed an issue with the combobox? I hope I'm clear with my question and this time I will get some help from this forum. ComboBox control has got selectedindexchanged event. As the name suggest this event should fire when selected item change for the combobox. But this event fires even when the currently selected item is selected again from the dropdown list of combobox. e.g. if I have loaded following items in my combobox say item1 item2 item3 item4 with item1 as selected when my application starts. Now if from UI, I again select the "item1" (which is already selected in combo box) then also the selectedindexchanged event fires. In this case the index has not changed still the selectedindexchanged event fires. I find this bit strange. Is there anyway to findout when the selected item has changed by User. I want some kind of notification when user indeed changes the combobox selected item (not when combobox.selecteditem is set through code)? regards KC
-
I had posted this question two weeks back but till date no one has responed. Is this indeed an issue with the combobox? I hope I'm clear with my question and this time I will get some help from this forum. ComboBox control has got selectedindexchanged event. As the name suggest this event should fire when selected item change for the combobox. But this event fires even when the currently selected item is selected again from the dropdown list of combobox. e.g. if I have loaded following items in my combobox say item1 item2 item3 item4 with item1 as selected when my application starts. Now if from UI, I again select the "item1" (which is already selected in combo box) then also the selectedindexchanged event fires. In this case the index has not changed still the selectedindexchanged event fires. I find this bit strange. Is there anyway to findout when the selected item has changed by User. I want some kind of notification when user indeed changes the combobox selected item (not when combobox.selecteditem is set through code)? regards KC
Try overriding the combobox control. Very, very basic example below;
class ComboBoxWidget: System.Windows.Forms.ComboBox { #region private member variables private int \_selectedIndex = -1; #endregion #region overrides protected override void OnSelectedIndexChanged( EventArgs e ) { if( this.SelectedIndex != this.\_selectedIndex ) { this.\_selectedIndex = this.SelectedIndex; base.OnSelectedIndexChanged( e ); } } public override int SelectedIndex { get { return base.SelectedIndex; } set { this.\_selectedIndex = value; base.SelectedIndex = this.\_selectedIndex; } } #endregion }