ErrorProvider



  1. To run this example, paste the following code into a form containing a TextBox named TextBox1, an OpenFileDialog named OpenFileDialog1, a button named Button1, and an ErrorProvider named ErrorProvider1. Ensure all events are associated with their event handlers.
  2. The Windows Forms ErrorProvider component is used to validate user input on a form or control. It is typically used in conjunction with validating user input on a form, or displaying errors within a dataset.

The .NET Windows Forms ErrorProvider component is used to validate user input on form controls. If a control failed validation, ErrorProvider displays an error icon next to the control and pops up a tooltip with an error message on hovering over the icon.

Download source files - 9 Kb; Download demo project - 23 Kb. In every GUI application, it is very likely that a user will enter some invalid data or perform some action that will make the state of a control invalid.

Since ErrorProvider is a non-visual control, TestComplete does not recognize it during the test recording. However, you can access the ErrorProvider component in your tests via the form’s native property and use the component’s native methods and properties to work with it.

Suppose that you use the ErrorProvider Edit vsdx file without visio. component to validate the data a user enters into the text box control and you want to check what text is returned by the ErrorProvider control in case of a data entry error. The following script demonstrates how you can do this. The example obtains the form that contains the text box control whose contents are being validated, accesses the ErrorProvider object and then returns the error text.

Note:To run this script from a keyword test, use the Run Script Routine operation.

JavaScript, JScript

function Test()
{
var frm, control, errorProviderObj, s;
// Obtain your form
frm = Sys.Process('WindowsFormsApplication1').WinFormsObject('Form1');
// Obtain the control for which you would like to obtain the error text
control = frm.textBox1;
// or
// Set control = frm.WinFormsObject('textBox1');
// Obtain the ErrorProvider component that is on the form.
errorProviderObj = frm.errorProvider1;
// Obtain the error text for the needed control.
s = errorProviderObj.GetError(control);
// Log result
Log.Message (s);
}

Python Utility software define.

VBScript

Sub Test
' Obtain your form
Set frm = Sys.Process('WindowsFormsApplication1').WinFormsObject('Form1')
' Obtain the control for which you would like to obtain the error text
Set control = frm.textBox1
' or
' Set control = frm.WinFormsObject('textBox1')
' Obtain the ErrorProvider component that is on the form.
Set errorProviderObj = frm.errorProvider1
' Obtain the error text for the needed control.
s = errorProviderObj.GetError(control)
' Log result
Log.Message s
EndSub

DelphiScript

Wpf Errorprovider

procedure Test;
var frm, control, errorProviderObj, s;
begin
// Obtain your form
frm := Sys.Process('WindowsFormsApplication1').WinFormsObject('Form1');
// Obtain the control for which you would like to obtain the error text
control := frm.textBox1;
// or
// Set control := frm.WinFormsObject('textBox1');
// Obtain the ErrorProvider component that is on the form.
errorProviderObj := frm.errorProvider1;
// Obtain the error text for the needed control.
s := errorProviderObj.GetError(control);
// Log result
Log.Message (s);
end;

C++Script, C#Script

Errorprovider vb.net

Errorprovider In C#

function Test()
{
var frm, control, errorProviderObj, s;
// Obtain your form
frm = Sys['Process']('WindowsFormsApplication1')['WinFormsObject']('Form1');
// Obtain the control, for which you would like to obtain the error text
control = frm['textBox1'];
// or
// Set control = frm['WinFormsObject']('textBox1');
// Obtain the ErrorProvider component that is on the form.
errorProviderObj = frm['errorProvider1'];
// Obtain the error text for the needed control.
s = errorProviderObj['GetError'](control);
// Log result
Log['Message'] (s);
}

See Also

Testing .NET Applications
Accessing Non-Visual Objects in .NET Applications

-->

You can use a Windows Forms ErrorProvider component to display an error icon when the user enters invalid data. You must have at least two controls on the form in order to tab between them and thereby invoke the validation code.

To display an error icon when a control's value is invalid

  1. Add two controls — for example, text boxes — to a Windows Form.

  2. Add an ErrorProvider component to the form.

  3. Select the first control and add code to its Validating event handler. In order for this code to run properly, the procedure must be connected to the event. For more information, see How to: Create Event Handlers at Run Time for Windows Forms.

    The following code tests the validity of the data the user has entered; if the data is invalid, the SetError method is called. The first argument of the SetError method specifies which control to display the icon next to. The second argument is the error text to display.

    (Visual C#, Visual C++) Place the following code in the form's constructor to register the event handler.

  4. Run the project. Type invalid (in this example, non-numeric) data into the first control, and then tab to the second. When the error icon is displayed, point at it with the mouse pointer to see the error text.

Errorprovider Wpf

See also