Solved

ErrorProvider Question

Posted on 2008-10-12
5
501 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now