5,696,576 members and growing! (18,851 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Intermediate License: The Code Project Open License (CPOL)

Extended error provider

By Shabdar Ghata

This article explains you how a basic Error Provider class can be extended to have more functionality
C#, VB.NET 1.1, WinXP, Windows, .NETVisual Studio, VS.NET2003, VS2005, Dev

Posted: 7 Oct 2005
Updated: 30 Jan 2008
Views: 28,424
Bookmarked: 11 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 2.15 Rating: 2.54 out of 5
2 votes, 28.6%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
5 votes, 71.4%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Sample Image - ErrorProviderExtended.gif

Introduction

This article explains you 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,

    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

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(<ControlName> as String,<DisplayName(Optional)>
as String,<ErrorMessage(Optional)> 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
        '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 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
        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 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.

Updates

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)

About the Author

Shabdar Ghata


Software Developer

http://www.shabdar.org
Occupation: Web Developer
Location: Canada Canada

Other popular VB.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
GeneralPassing the code to C#membercarodiu12:19 16 Jul '07  
AnswerRe: Passing the code to C#memberShabdar Ghata15:14 16 Jul '07  
GeneralRe: Passing the code to C#membercarodiu5:55 17 Jul '07  
GeneraldoubtmemberRafeeque Ahmed3:44 15 Jul '07  
GeneralRe: doubtmemberRafeeque Ahmed4:30 17 Jul '07  
Generalconvert this to c#?memberTheCardinal20:38 6 Aug '06  
GeneralI dont understand this C#memberleppie12:11 7 Oct '05  
GeneralRe: I dont understand this C#memberShabdar Ghata14:40 7 Oct '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Jan 2008
Editor:
Copyright 2005 by Shabdar Ghata
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project