DataBinding Question

D

dvestal

The example below creates a ComboBox, and binds its Text field to the
Text field of a textbox. When the user types into the textbox, the
corresponding ComboBox value is selected.

What is the best way to know at runtime whether the user has typed a
value that isn't in the ComboBox's list of valid values? I need a
solution that works using the built-in binding events, rather than
using manual checking of the value when it changes.

I'd like to set an ErrorProvider to alert the user to the invalid
value.

using System;
using System.Windows.Forms;

namespace A
{
public class C
{
[STAThread]
static void Main()
{
Form form = new Form();

TextBox txt = new TextBox();
txt.Top = 10;
txt.Left = 10;
txt.Text = "1";

ComboBox cbo = new ComboBox();
cbo.Top = txt.Bottom + 10;
cbo.Left = 10;
cbo.DropDownStyle = ComboBoxStyle.DropDownList;
cbo.Items.AddRange(new object[]{ 1, 2 });

cbo.DataBindings.Add("Text", txt, "Text");

form.Controls.Add(txt);
form.Controls.Add(cbo);

form.ShowDialog();
}
}
}
 
A

AlexS

Best way depends on how you detect that user has finished typing.

Otherwise you might handle text changed event for text box and filter combo
list for matches. If at any time match list is empty, you can display your
message box.

What do you mean by "using binding events"?
 
D

dvestal

Best way depends on how you detect that user has finished typing.

Otherwise you might handle text changed event for text box and filter combo
list for matches. If at any time match list is empty, you can display your
message box.

What do you mean by "using binding events"?

I mean that I'd much rather use the Binding.Format, Binding.Parse, or
Binding.BindingComplete events to check this, or some other binding-
specific event of which I'm unaware.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top