Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a combobox with the event SelectedValueChanged associated with a method. I have found how odd it works when I select the old value (the value displayed as its Text property). It raises the event and calls the method!

I'm using a combobox with DropDownStyle = DropDownList, AutoCompleteMode = SuggestAppend and AutoCompleteSource = ListItems. How do I prevent this from happening? I don't want it that way!

It doesn't seem to conform to the name of the event "SelectedValueChanged" that means if the selected value doesn't change, nothing will happen! Thank you so much!
Posted
Updated 9-Apr-11 14:45pm
v3
Comments
Sergey Alexandrovich Kryukov 9-Apr-11 20:31pm    
Tag it: WPF, WinForms, what?
--SA
Nish Nishant 9-Apr-11 20:45pm    
Pretty sure he means Winforms. I've improved his post to add that tag!
Sergey Alexandrovich Kryukov 9-Apr-11 20:34pm    
I never faced any abnormality in such cases.
Make a minimal code to show the problem and post it. Don't post the code of your application. Please make a special demo to focus just on the problem.
--SA

Yeah, that is standard behavior with the ComboBox (in Winforms). Once you drop down the control, then even if you select the same item that was previously selected, SelectedValueChanged will fire! I reckon you'd have to keep a currentSelectedValue and then compare against that each time it fires.

(Note that it's same for SelectedIndexChanged as well)

[Edit]
---------

Just wanted to add that this assumes you are using a databound combobox. I have not checked this behavior with a non-databound combobox with manually added items.
 
Share this answer
 
v2
Comments
[no name] 10-Apr-11 0:37am    
Thank you! :)
Sergey Alexandrovich Kryukov 10-Apr-11 1:11am    
Very good, you explained how it works, all correct, my 5. In my answer, I added motivation of it and a simple practical advice what OP can do about it, please see.
--SA
Wendelius 10-Apr-11 2:01am    
Well explained, my 5.
Shahin Khorshidnia 10-Apr-11 2:22am    
+5
Michel [mjbohn] 10-Apr-11 4:35am    
my 5
If you read the MSDN page[^] for SelectedValue property you will see that the 'value' it is referring to is the value of the ValueMember property. So it may not actually be the event that you need if you are not using data binding.

This doesn't explain the problems you are seeing, of course but it mat be that you should be using the SelecetedIndexChanged event.
 
Share this answer
 
Comments
[no name] 10-Apr-11 0:39am    
Thank you! What a helpful suggestion! I should try it! Thank you so much!
Sergey Alexandrovich Kryukov 10-Apr-11 1:12am    
Correct, a 5. I actually explained it and what OP can do about it.
--SA
Wendelius 10-Apr-11 2:01am    
Good explanation, my 5
Espen Harlinn 10-Apr-11 6:34am    
Nice link, my 5
OK, Nishant explained how it works. I just want to add couple of practical notes.

Actually selection is not fired if you simply move selection of the list box. It is fired when you confirm the selection by Enter or dbl-click, so it actually shown in the edit box part of the combo. It is quite reasonable to fire this event of confirmation, no matter is that new or old value.

You can easily get the effect you need: in the event handler, remember old value and compare new value with the old one. If they are the same — do nothing, exit the handler immediately. As simple as that.

—SA
 
Share this answer
 
Comments
[no name] 10-Apr-11 1:37am    
With my programming skill, I could think of that solution with comparing old value to the new. But I have not only combobox but about dozens of such comboboxes. That means I need a corresponding number of variable or an array to store all old values. It makes me crazy! So I want another solution! I also tried with "SelectedIndexChanged" but it did the same as "SelectedValueChanged"!
I have found that the appropriate solution for me is using "TextChanged". It can really help me!
Thank you!
Sergey Alexandrovich Kryukov 10-Apr-11 1:59am    
What's the difference, 1 combo box or 90000? No difference. Do it just once, created a class based on ComboBox, and use you derived class instead of. Big deal!
--SA
[no name] 10-Apr-11 1:42am    
Oh no, it only helps me when changing to new value by selecting from dropdown list but when pressing any key (with autocompletemode) it doesn't fire! Maybe I should use it with one of SelectedValueChanged or SelectedIndexChanged!
Sergey Alexandrovich Kryukov 10-Apr-11 2:00am    
If you concerned about selected index in first place, handle this event. I don't see what could be a problem...
--SA
Wendelius 10-Apr-11 2:02am    
Good addition, my 5

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