Problem with combobox in Winform and C# when using keyboard

dominicwong
dominicwong used Ask the Experts™
on
Hi experts
I have a combobox populated with numbers:      
        cbColorCode.Items.AddRange(choices);

When I changed the selection via a MOUSE, the 'SelectedIndex', 'SelectedItem', and 'Text' were all correctly updated.

BUT, when I changed the selection via a KEYBOARD, only the 'Text' got updated. The other two remained unchanged. Therefore, when I left the TabPage (where this combobox is) and came back, the value was reverted. (Probably because the combobox was bound by its 'SelectedIndex')

Could someone tell me how to resolve it? It is fine with mouse though. Only when I used keyboard then I have this problem.

Note:
This combobox is bound to a Data Source.
The binding source is bound to an object.

cbColorCode.DataBindings.Add("SelectedIndex", bindingSource, "ColorCode", true, DataSourceUpdateMode.OnPropertyChanged, "-1");
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Meir RivkinFull stack Software Engineer

Commented:
i tried to put combobox on winform an play with the SelectedIndexChanged event handler both mouse and keyboard behaves the same.
the SelectedIndex,SelectedItem and Text  are updated.
can u post the code of the whole binding?

Author

Commented:
Hi sedgwick

Thanks for your prompt response.
I didn't use any event handler to handle the combobox change. It is handled internally by the data binding code inside .NET.

Therefore, the only thing I need to do is:
cbColorCode.DataBindings.Add("SelectedIndex", bindingSource, "ColorCode", true, DataSourceUpdateMode.OnPropertyChanged, "-1");

Whenever the combobox selection is changed, the bound data object (bound by the binding Source) automatically get updated.

It works fine with mouse as the 'SelectedIndex' was changed, but not with keyboard as only the 'Text' was changed.
When you say you are using the keyboard, does that mean that you are typing in the number, and NOT using the arrow keys to scroll through the combo box?

AW
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Author

Commented:
Hi Arthur, yes, that was what I did. I entered a number via keyboard, instead of using mouse.
That does NOT cause the Change event on the ComboBox to fire.  Yes, that's what I said.  The control does not see the new value as a change.  You have not caused the selexcted Index to change.  The only way to do that is to actually scroll the combo box, eiterh by using the mouswe, or by using the up or down arrow keys on the keyboard.

Author

Commented:
Hi Arthur
The issue is that the combo box allows user to enter text, and user expects the same behavior from either a mouse or directly enters the text. Or, perhaps I need to add an event handler to handle that? Do you know, by any chance, which event handler? Thanks.
Change the style of the Combo Box to DropDownList ... Then the value will not be editable.  The user can only use the mouse or the arrow keys to change the selection.

AW

Author

Commented:
I can't see any 'DropDownList' option under the ToolBox. Please enlighten.

By the way, if the Combo Box was meant to allow user to directly enter text, there must a way to handle it properly. Do you know, by any chance, what that is? Thanks.
right click on your combo box, in design mode, and choose properties.  Under DropDownStyle, choose the DropDownList option.

Alternatively, you can add an Event Handler for the TextChanged event, but that would mean that you would have to handle EACH AND EVERY KEYSTROKE that the user makes when accessing the Combo Box - and that could become a real pain, if the user is typing is a long color code.  Besides, I suspect that you want the user to select one of the color codes that are given in the list, and not to be able to add new color codes "on the fly".

AW
Top Expert 2016
Commented:
if you change to dropdown list you nevertheless can type the numbers for faster access. it isn't an edit change then but the key typed was used to locate the first entry in the combobox that begins with the typed key. however if you type slowly each key stroke would mean a new search what rarely is that what is intended.

a handler for the text changed simply would do a FindString operation in the combobox with the text typed so far. if that returns a valid index greater or equal to 0 you actively would assign that index to yourCombo.selectedIndex .

Sara

Author

Commented:
Thanks Arthur and Sara.

I'd changed the style of the combobox into a 'DropDownList' as per Arthur's advice.
I didn't need to add any event handler. I entered text directly into the combobox and the 'SelectedIndex' is automatically updated (I don't know how it did that).

For experiment, I'd added an event handler for 'TextChanged' and I print out the Text and the SelectedIndex. They were both updated automatically. So it seems to me that just by changing the style into a 'DropDownList' is good enough to resolve this issue.

Not sure it has anything to do with my databinding code (as mentioned before):
cbColorCode.DataBindings.Add("SelectedIndex", bindingSource, "ColorCode", true, DataSourceUpdateMode.OnPropertyChanged, "-1");
Glad to be of assistance.

AW

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial