binding combobox.text not compatible with arrow keys?
I have a combobox manually filled with a couple of items. I want to bind the combo to a dataset to store the value selected. As there is no datasource for the combo and no valuemember set, I am binding to the text property rather than the selectedvalue.
Is this right?
Whilst this works fine when selecting an item from the combo using the mouse, if I use the keyboard (arrow keys), the text in the combo changes, but the change is not registered with the bindings, so when I come to save, I get a nullexception. I believe that binding to the text property uses the textchanged event which after checking, does not get fired when using the arrow keys.
Is this the correct behaviour?
What can I do to allow selection using the arrow keys?
Thanks
[756 byte] By [
Zaph0d] at [2008-2-5]
If you are trying to create a lookup table with your combobox, whereby you present the list of choices, see
ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxmclidata/html/419aac5e-819b-4aad-88b0-73a2f8c0bd27.htm
in the VS Help (or, look for "binding data, Windows Forms", and find the link there under the Lookup Table paragraph. Basically, this describes how to configure two bindings for the combobox or listbox: 1) the bindings for the list of choices, and 2) the binding for the selected value. Your list of possible choices could be from an array list or the like, rather than a database, so you can still manually load up the list of choices.
Sorry - the sample only works for one way binding. For two-way binding, it may get more complex depending on what you need. If you don't mind your combo box strings and your data source strings being different, then you can bind to the SelectedIndex property. If that won't work, then you'll have to either create a wrapper object that wraps your string and makes the string available through a "Value" property or bind to the SelectedIndex and write Format/Parse events (snippet below - this replaces the binding lines from the sample I sent before).
Joe
| | Binding b = new Binding("SelectedIndex", tb, "Text", true, DataSourceUpdateMode.OnPropertyChanged); b.Format += delegate(object send, ConvertEventArgs args) { args.Value = cb.FindStringExact(args.Value as string); }; b.Parse += delegate(object send, ConvertEventArgs args) { args.Value = cb.SelectedItem; }; cb.DataBindings.Add(b); |