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]
# 1
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.

durstin at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
The help url does not work for me using VS express help and I cannot find any reference to "binding data, Windows Forms" either.

Are you saying that you should always bind to some type of datasource even if the combo only contains YES and NO?

Zaph0d at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

You can still use the SelectedValue but you'll need to bind the ComboBox to a string list. The following example should help.

Joe


private void Form1_Load(object sender, EventArgs e)
{
ComboBox cb = new ComboBox();
TextBox tb = new TextBox();
List<string> ls = new List<string>();

ls.Add("Yes");
ls.Add("No");
ls.Add("Maybe");

cb.Location = new Point(10, 10);
cb.DataSource = ls;
cb.DropDownStyle = ComboBoxStyle.DropDownList;
this.Controls.Add(cb);

tb.Location = new Point(10, 40);
this.Controls.Add(tb);

cb.DataBindings.Add("SelectedValue", tb, "Text", true, DataSourceUpdateMode.OnPropertyChanged);
}

JoeStegman at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
whilst your example works, when I attempt to integrate it into my solution, I get the error "Cannot set the SelectedValue in a List Control with an empty ValueMember"
Zaph0d at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

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);

JoeStegman at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...