Click here to Skip to main content
15,868,292 members
Articles / Programming Languages / Visual Basic

Extended Error Provider

Rate me:
Please Sign up or sign in to vote.
2.87/5 (9 votes)
5 Mar 2010CPOL2 min read 90.5K   2K   32   13
This article explains how a basic Error Provider class can be extended to have more functionality.
Sample Image - ErrorProviderExtended.gif

Introduction

This article explains how a basic Error Provider class can be extended to include more functionality. This extended class can reduce coding significantly for validating mandatory objects on Windows Form.

Features

  1. Validate multiple controls without writing events for each control separately.

    If you use basic Error Provider, you need to write validating event for each control that you want to check. For example, for checking txtStudentName text box control, you might write code as below:

    VB.NET
    Private Sub txtStudentName_Validating(ByVal sender As Object, _
    	ByVal e As System.ComponentModel.CancelEventArgs) _
    	Handles txtStudentName.Validating
        If txtStudentName.Text = "" Then
            ErrorProvider1.SetError(txtStudentName, "Please enter student name")
        Else
            ErrorProvider1.SetError(txtStudentName, "")
        End If
    End Sub

    You need to write the same event for each control that you want to validate. It increases code significantly in bigger projects.

    But with extended error provider, all you need to write is just:

    VB.NET
    MyErrorProvider.Controls.Add(txtStudentName, "Student Full Name", _
    	"Please enter student name")
  2. Display custom message box.

    With extended error provider, you can set custom error message box for missing fields which are mandatory.

  3. Conditional validation of controls.

    You can enable or disable validation control based upon certain conditions.

Usage of ErrorProviderExtended class

  1. Method for adding controls:
    VB.NET
    MyErrorProvider.Controls.Add(<ControlName> as String,<DisplayName(Optional)>
    as String,<ErrorMessage(Optional)> as String)
  2. Setting summary message:
    VB.NET
    MyErrorProvider.SummaryMessage = "Following fields are mandatory,"
    
  3. Enabling/Disabling control validation:
    VB.NET
    MyErrorProvider.Controls(txtEmergencyContact).Validate = False
    

Using the Code

Open ErrorProvidelExtended.sln in Visual Studio .NET. There are two projects inside ErrorProvidelExtended.sln. The first project is Extended Error Provider class. The second project is a sample usage for extended error provider.

Using the extended error provider is fairly simple. The description of code is included as comments.

VB.NET
'Declare a variable
Dim MyErrorProvider As New ErrorProviderExtended

    Private Sub TestForm_Load(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles MyBase.Load
        'Add controls one by one in error provider.
        MyErrorProvider.Controls.Add(txtStudentName, "Student Full Name")
        MyErrorProvider.Controls.Add(txtStudentAge, "Age")
        MyErrorProvider.Controls.Add(txtEmergencyContact, "Emergency Contact Number")
        'Initially make emergency contact field as non mandatory
        MyErrorProvider.Controls(txtEmergencyContact).Validate = False
        'Set summary error message
        MyErrorProvider.SummaryMessage = "Following fields are mandatory,"
    End Sub

    Private Sub cmdSubmit_Click(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles cmdSubmit.Click
        'Following function checks all empty fields and returns TRUE 
        'if all fields are entered.
        'If any mandatory field is empty this function displays a message 
        'and returns FALSE.
        If MyErrorProvider.CheckAndShowSummaryErrorMessage = True Then
            MessageBox.Show("Data submitted successfully.")
        End If
    End Sub

    Private Sub chkAge_CheckedChanged(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles chkAge.CheckedChanged
        If chkAge.Checked Then
            'if student's age is less than 10, emergency contact is mandatory
            MyErrorProvider.Controls(txtEmergencyContact).Validate = True
        Else
            'if student's age is greater than 10, emergency contact is not mandatory
            MyErrorProvider.Controls(txtEmergencyContact).Validate = False
        End If
    End Sub
End Class

Description of ErrorProviderExtended class is included as comments in the source code.

Points of Interest

I have used an extended collection base class for ErrorProviderExtended.Controls property. It uses the ValidationControlCollection class which is derived from the CollectionBase class. Each control is of type ValidationControl. ValidationControl class has all required properties like DisplayName, ErrorMessage, Validate, ControlObj, etc.

Updates

The source code is now available in C# (Visual Studio 2005) edition.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
Questionneed Help! Pin
Serginho Silva2-Jun-13 14:08
Serginho Silva2-Jun-13 14:08 
Questionnot clearing icon Pin
ivanmdeb10-May-12 21:31
ivanmdeb10-May-12 21:31 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey12-Mar-12 22:27
professionalManoj Kumar Choubey12-Mar-12 22:27 
Nice
GeneralNice job Pin
kungming9-Aug-11 5:01
kungming9-Aug-11 5:01 
QuestionDownload?? Pin
kapil bhavsar16-Apr-09 2:10
kapil bhavsar16-Apr-09 2:10 
GeneralPassing the code to C# Pin
carodiu16-Jul-07 11:19
carodiu16-Jul-07 11:19 
AnswerRe: Passing the code to C# Pin
User 99267416-Jul-07 14:14
User 99267416-Jul-07 14:14 
GeneralRe: Passing the code to C# Pin
carodiu17-Jul-07 4:55
carodiu17-Jul-07 4:55 
Generaldoubt Pin
Rafeeque Ahmed15-Jul-07 2:44
Rafeeque Ahmed15-Jul-07 2:44 
GeneralRe: doubt Pin
Rafeeque Ahmed17-Jul-07 3:30
Rafeeque Ahmed17-Jul-07 3:30 
Questionconvert this to c#? Pin
TheCardinal6-Aug-06 19:38
TheCardinal6-Aug-06 19:38 
GeneralI dont understand this C# Pin
leppie7-Oct-05 11:11
leppie7-Oct-05 11:11 
GeneralRe: I dont understand this C# Pin
User 9926747-Oct-05 13:40
User 9926747-Oct-05 13:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.