Export (0) Print
Expand All

How to: Display Error Icons for Form Validation with the Windows Forms ErrorProvider Component

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.

    Private Sub TextBox1_Validating(ByVal Sender As Object, _
       ByVal e As System.ComponentModel.CancelEventArgs) Handles _
       TextBox1.Validating
          If Not IsNumeric(TextBox1.Text) Then
             ErrorProvider1.SetError(TextBox1, "Not a numeric value.")
          Else
             ' Clear the error.
             ErrorProvider1.SetError(TextBox1, "")
          End If
    End Sub
    
    

    protected void textBox1_Validating (object sender,
       System.ComponentModel.CancelEventArgs e)
    {
       try
       {
          int x = Int32.Parse(textBox1.Text);
          errorProvider1.SetError(textBox1, "");
       }
       catch (Exception e)
       {
          errorProvider1.SetError(textBox1, "Not an integer value.");
       }
    }
    
    

    protected void textBox1_Validating(Object sender, CancelEventArgs e) 
    {
       try 
       {
          int x = Int32.Parse(textBox1.get_Text());
          errorProvider1.SetError(textBox1, "");
       }
       catch(Exception exp)
       {
          errorProvider1.SetError(textBox1, "Not an integer value.");
       }
    }
    
    

    private:
       System::Void textBox1_Validating(System::Object ^  sender,
          System::ComponentModel::CancelEventArgs ^  e)
       {
          try
          {
             int x = Int32::Parse(textBox1->Text);
             errorProvider1->SetError(textBox1, "");
          }
          catch (System::Exception ^ ex)
          {
             errorProvider1->SetError(textBox1, "Not an integer value.");
          }
       }
    

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

    this.textBox1.Validating += new
    System.ComponentModel.CancelEventHandler(this.textBox1_Validating);
    
    

    this->textBox1->Validating += gcnew
       System::ComponentModel::CancelEventHandler
       (this, &Form1::textBox1_Validating);
    
  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.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft