Extended Error Provider using VB.Net and C#.Net

This article describes how a basic Error Provider class can be extended to include more functionality. This extended class can reduce significant coding for validating objects on windows form.

Download Source Download Source (VB.Net - VS 2003)
Download Source Download Source (C#.Net - VS 2005)


1) Allows you to 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,

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")
 ErrorProvider1.SetError(txtStudentName, "")
 End If
 End Sub

Same event you need to write 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,

 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

MyErrorProvider.Controls.Add( as String, as String, as String)

2) Setting summary message

MyErrorProvider.SummaryMessage = "Following fields are mandatory,"

3) Enabling/Disabling control validation

MyErrorProvider.Controls(txtEmergencyContact).Validate = False

Using the code

Open ErrorProvidelExtended.sln solution in Visual Studio.Net. There are two projects inside ErrorProvidelExtended.sln solution. First project is Extended Error Provider class. Second Project is a sample usage for extended error provider.

Using extended error provider is fairly simple. Description of code is included as comments.

'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

<blockquote>'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 

<blockquote>'Following function checks all empty fields and returns TRUE if all fields are entered.
 'If any mandotary field is empty this function displays a message and returns FALSE.
 If MyErrorProvider.CheckAndShowSummaryErrorMessage = True Then
 MessageBox.Show("Data submited successfully.")
 End If
End Sub

 Private Sub chkAge_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAge.CheckedChanged 

<blockquote>If chkAge.Checked Then
 'if student's age is less than 10, emergency contact is mandatory
 MyErrorProvider.Controls(txtEmergencyContact).Validate = True
 '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 source code.

Points of Interest

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


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

SpecialĀ  Notes

I have published this article on codeproject.com as well. Here is the link to this article.


Posted: 9 years 2 weeks ago by Jaypee #132
Jaypee's Avatar
this is great article !!!!! thanks for sharing with us.
check out the following url for another simplest one article related to this article...


Thanks !!!!