Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ErrorProvider Question

Posted on 2008-10-12
5
Medium Priority
?
523 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 2000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

704 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