Solved

ErrorProvider Question

Posted on 2008-10-12
5
518 Views
Last Modified: 2012-05-05
Hi, I'm using VB 2005, WinForms. I have a question about the ErrorProvider Control. -- Lets assume I've used the ErrorProvider in my Form on 15 different Controls, so that I can validate the input of each Control (assume it all works well). I don't want to add a new record unless all the validations have passed my requirements. So, how do I assess whether there are any validation tests that have not yet been addressed. In other words, how can I tell that an ErrorProvider is still alerting my User that a problem exists with one of the Controls?

Is there some sort of ErrorProvider.Count property (which I know is not available) or something similar that I can use to determine if I still have problems that need to be addressed?

By the same token, I want to be able to know that if no problems exist, I can go ahead and perform my record insertion into my DataBase.

So, in essence, I'm looking for some sort of "flag" that the ErrorProvider is still alerting of errors on the Form.

Thanks,
Fulano
0
Comment
Question by:Mr_Fulano
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 22700410
The Error Provider Component used on Windows Forms is a very useful way of displaying the errors on the form, however its missing one small piece of information; the number of errors that are currently on the form. This custom error provider fixes that problem and allows you, among other things, to check the number of errors on the form before performing some action. i.e. If Me.ErrorProvider.HasErrors = False then It's proved very useful in a project

Source: http://dotnetslackers.com/Community/blogs/dsmyth/archive/2007/10/12/custom-error-provider.aspx
Public Class ErrorProviderWithCount
Inherits System.Windows.Forms.ErrorProvider
 
Dim controlsWithErrors As New List(Of Windows.Forms.Control)
 
Public Sub New(ByVal components As System.ComponentModel.IContainer)
MyBase.New(components)
End Sub
 
Public Shadows Sub SetError(ByVal control As Windows.Forms.Control, ByVal value As String)
MyBase.SetError(control, value)
If String.IsNullOrEmpty(value) Then
If controlsWithErrors.Contains(control) = True Then
controlsWithErrors.Remove(control)
End If
Else
If controlsWithErrors.Contains(control) = False Then
controlsWithErrors.Add(control)
End If
End If
End Sub
 
Public ReadOnly Property Count() As Integer
Get
Return controlsWithErrors.Count
End Get
End Property
 
Public ReadOnly Property HasErrors() As Boolean
Get
Return controlsWithErrors.Count > 0
End Get
End Property

Open in new window

0
 

Author Comment

by:Mr_Fulano
ID: 22700457
Hi Dhaest, thanks for the code. It an interesting approach. However, I did some research after I posted the question and found that if I use the ErrorProvider.GetErrro(myControl).Length property I can determine if the control still has an error. So in a simple IF Statement, you would do:

If ErrorProvider.GetErrro(myControl).Length > 0 Then
.. do something like set a flag or provide a message to the User.
End If

The down side is you have to check the length of each control, but it works well without having to build a new control.

Regardless, thanks for the code, which is always useful.
Fulano
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 22700461
>> The down side is you have to check the length of each control, but it works well without having to build a new control.

I also knew that, but suppose you have 30 different controls, you'll have a big if-statement :)
Anyway, glad to see it's solved
0
 

Author Comment

by:Mr_Fulano
ID: 22700466
Correction for the typo above. The code should read:

ErrorProvider.GetError(myControl).Length

GetError not GetErrro

: )
0
 

Author Comment

by:Mr_Fulano
ID: 22700470
Yes, I agree with you that the IF statement can get lengthy, but it does fast once you get started.

Thanks again,
Fulano
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question