DataBinding Question

  • Thread starter Thread starter dvestal
  • Start date Start date
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();
}
}
}
 
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"?
 
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.
 
Back
Top