Skip to content

AppHelp

ErrorProvider Component (Windows Forms)

The Windows Forms ErrorProvider component is used to show the user that something is wrong. It is typically used in conjunction with validating user input on a form, or displaying errors within a dataset.

Windows Forms supports ErrorProvider help on controls for data input validation. Normally validation doesn't go much further than a check of the maximum length for textboxes. But this is a nice method you can use to make validation a breeze.

ErrorProvider AppHelp

When a validation error appears the red error icon occurs and one can hover it for a tooltip with information about the invalid field. The error icon method is much better than a message box pop-up, because that would be an unnecessary interruption. So we get a method of unobtrusively showing that data is invalid, and presenting it in such a way that it is immediately obvious what is wrong and what to do about it.

To provide ErrorProvider help in your application, you have to use the ErrorProvider component which you can find in the Toolbox on the Windows Forms tab. Drag and Drop a ErrorProvider component from the Toolbox to your form. It is not a control but a component, so when you put it on your form it will sit in the component tray below. This component gives every control on the form a new property called Error on ErrorProvider1.

ErrorProvider AppHelp

You can set the “Error on ErrorProvider1” property of the txtAddressZipCode field, but this does not really correspond to the intention of the ErrorProvider component. You will see the red symbol in the development environment, but if you do not want to perform a validity check via code, it is better to use the ToolTip component.

ErrorProvider AppHelp

The better idea is to create an event handler for data input validation on the form and add the following code to it. To clear the error , simply pass an empty string String.Empty.

ErrorProvider code example

Private Sub cmdInputValidation_Click(sender As Object, e As EventArgs) Handles cmdInputValidation.Click
    Dim sAddressEmail As String = txtAddressEmail.Text
    Dim sAddressZipCode As String = txtAddressZipCode.Text
    Dim sAddressTownName As String = cboTownName.Text
    Dim sMessage As String
    Dim IsValidationError As Boolean = False

    '----- set error message when @ is missing -----
    If InStr(sAddressEmail, "@") = 0 Then
        ErrorProvider1.SetError(txtAddressEmail, "Missing '@' - Please enter the complete e-mail address")
        IsValidationError = True
    Else
        ErrorProvider1.SetError(txtAddressEmail, String.Empty)
    End If

    '----- input validation of ZipCode -----
    If Not IsNumeric(sAddressZipCode) Or Len(sAddressZipCode) <> 5 Then
        ErrorProvider1.SetError(txtAddressZipCode, "Please enter 5 digits")
        IsValidationError = True
    Else
        ErrorProvider1.SetError(txtAddressZipCode, String.Empty)
    End If

    '----- set error message when len = 0 -----
    If Len(Trim(sAddressTownName)) = 0 Then
        ErrorProvider1.SetError(cboTownName, "Please enter the town name")
        IsValidationError = True
    Else
        ErrorProvider1.SetError(cboTownName, String.Empty)
    End If

    '--- Continue when all the text boxes are valid
    If IsValidationError = True Then
        sMessage = sAddressEmail & " " & sAddressZipCode & " " & sAddressTownName
        MessageBox.Show(sMessage, "Error occured - Stop processing code")
        Exit Sub
    End If

    sMessage = sAddressEmail & " " & sAddressZipCode & " " & sAddressTownName
    MessageBox.Show(sMessage, "No Error occured - Continue processing code")

End Sub

ErrorProvider Special Features

Note

You can have a second ErrorProvider on your form e.g. with a yellow icon (16x16 developer defined) rather than red, for when a field is valid but could be entered better.

ErrorProvider AppHelp