Solved

ErrorProvider Question

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

16 Experts available now in Live!

Get 1:1 Help Now