dotnetmonitor.com |
|
||||||||||||
| In VS 2005 there are
two ways you can provide feedback to a user when a binding error occurs. The
easiest way is to add an ErrorProvider to the Form and set
ErrorProvider.DataSource to the same data source used by the Binding. In
addition, you can also add a handler to the BindingComplete event for the
Binding and write your own customer error handling logic. In the sample below,
the custom logic shows a ToolTip:
/******************************************************************* * Setup (using the Visual Studio Form designer): * * Add 1 TextBox to the Form (textBox1) * Add 2 CheckBoxes to the Form (showErrorToolTipCB * and showErrorProviderCB) * * Add the following public property to the Form class: * * private int _intValue; * * public int IntegerValue * { * get { return _intValue; } * set { _intValue = value; } * } * * Add the following code to the Form.Load event: ******************************************************************/
ToolTip errorTT = new ToolTip(); ErrorProvider ep = new ErrorProvider(this); Binding tbBinding = new Binding("Text", this, "IntegerValue", true);
/* Add binding */ this.textBox1.DataBindings.Add(tbBinding);
/* Hook BindingComplete to allow navigation */ tbBinding.BindingComplete += delegate(object bind, BindingCompleteEventArgs args) { if ((this.showErrorToolTipCB.Checked) && !String.IsNullOrEmpty(args.ErrorText)) { /* Show ToolTip */ errorTT.Show(args.ErrorText, this.textBox1, 2000); } };
this.showErrorProviderCB.CheckedChanged += delegate { ep.DataSource = (this.showErrorProviderCB.Checked ? this : null); };
/* Initialize ToolTip */ errorTT.IsBalloon = true; errorTT.ToolTipTitle = "Binding Error:";
/* Initialize ErrorProvider */ ep.BlinkStyle = ErrorBlinkStyle.NeverBlink; } |
||||||||||||
|
||||||||||||