Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I really don't understand how it happens? I have a combobox with only 2 values in its Items list. I have a method associating with the SelectedValueChanged event of the combobox, but I have discovered that It can raise for 2 times whenever I change the selected value by choosing 1 of only 2 values of the combobox. While raising the event and calling the corresponding method associating with it, its value doesn't change (the value I chose). The value doesn't change after I change it but SelectedValueChanged raises for 2 times, one for when I finish changing its value, but when the other is for? My combobox has only 2 values that can be selected, if SelectedValueChanged raises consecutively for 2 times, it should return with its value before( for example, its values are 0 and 1, at first the selected value is 0, after 2 times the event raised it should be 0 not 1 in my case) but it doesn't. Can't understand. Could you please give me some idea on how and when it is possible.

Plus, in fact I have some other comboboxes whose the SelectedValueChanged event is associated to the same method. And when changing these comboboxes' values, the event normally fire only once, but how do they differ from the odd combobox I forementioned? My odd combobox has not any databindings or any association that could make its value change except by selecting one from dropdown list!
Thank you so much!
Posted
Updated 11-Feb-20 11:10am
v4

Wow! I confidently say that I have found the answer to my own question by myself. That is simply.
I associated a method to the "odd" comboBox's SelectedValueChanged event twice!!! That's why when the event fires, the method will be called twice. If you do the same with three, four or more times it will be call exactly for such time. So if you have the same problem, recheck to see if you have associated the method more one time with the event, of course, there may be other causes.
Haha, I got it!
Thanks to myself!
 
Share this answer
 
Comments
Henry Minute 9-Apr-11 16:02pm    
Well done for finding the cause and well done for reporting it here for the benefit of others.

I have to say that this is not something that I would have thought to suggest as an answer, as it is not something that I have ever done.

Have 5 reputation points.
[no name] 9-Apr-11 16:14pm    
Thank you! Hihi! Just for my hard tries with a loop of thinking, editing and testing!
Have a look at this thread[^]. It may help you out.
 
Share this answer
 
Comments
[no name] 9-Apr-11 14:53pm    
Perhaps for my low level, I couldn't find any help with that thread! Hihi!
I'm going crazy with a mass of errors waiting for me in the future while coding and working around my project, it is obsessing me all the time! Oh my God! How could I carry on! I'm still a student with many other intersting subjects, I don't want to stop all them for this project. Bless me and Bless all who likes me.
I know you've solved this now but here's a hint for the future. Unless you dynamically create a control, it's well recommended to always use the designer to add event handlers. This way you will not accidentally add it twice. You may not be able to follow this rule everywhere, so if there are scenarios where you absolutely must add it in user written code, then make sure you do it all in one place, so it's easier to debug.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Apr-11 22:27pm    
Nishant, you probably already know I would strongly disagree.
Designer can make a lot more mess, and it also generate the syntax which is harder to support.
Accidentally adding extra event handler manually can be a result of sloppiness in the design and code which could entail much worse problems that this simple one. Also, adding multiple handlers to the same event is important feature having number of uses.
Respectfully,
--SA
Nish Nishant 10-Apr-11 0:01am    
Yeah, and I disagree with you SA :-)

As far as possible, when doing WinForms I exclusively use the designer to add event handlers to design time controls. It keeps everything in a single place and is easier to manage.
[no name] 10-Apr-11 1:58am    
I may agree with SA, using the designer is useful when events of various controls need to be associated with various methods. But I have hundreds of controls (comboboxes in my app) that act the same way and can use the same method as an event handler. I have to use a loop to associate their events to the same method! How hard to do that by selecting the same method for each control's event using Properties Window!
Anyway, I have learned when using designer and when coding it!
Thank you!
Nish Nishant 10-Apr-11 9:22am    
Yep, yours is a special scenario. But as you found out the hard way you did make a silly mistake there (which you luckily figured out soon enough). So for scenarios where you can use the designer, I would recommend that way.
Just for future reference for anyone else who lands here, I've also encountered this error in WPF when a bound ItemsSource collection is modified within the SelectionChanged event handler. For example, if you add or remove a value to be displayed in the combo-box you could cause the selection to inadvertently change thus firing the event again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900