Using ErrorProvider

  • Thread starter Thread starter M O J O
  • Start date Start date
M

M O J O

Hi,

How do I check if an ErrorProvider has errors?

Take for example this code...

(err = ErrorProvider...)

Public Sub Test
If TextBox1.Text= "" Then
err.SetError(TextBox1, "Error1'.")
End If

If TextBox2.Text= "" Then
err.SetError(TextBox2, "Error2'.")
End If


' Here I need to check if the err (ErrorProvider) has any
' errors. I need some kinda function like err.HasErrors, so I
' can exit the sub now if errors occurs.

..
..
Call SaveMyData
End Sub

Is there a way to test the ErrorProvider to see if it has erros?

Thanks!

M O J O
 
* M O J O said:
How do I check if an ErrorProvider has errors?

Untested:

\\\
If ErrorProvider1.GetError(Me.TextBox1).Length > 0 Then
...
End If
///
 
Hi Mojo,

Thanks for using Microsoft MSDN Managed Newsgroup. My name is Peter, and I
will be assisting you on this issue.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to trap if an
ErrorProvider has errors.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

I agree with Herfried's suggestion. I also has made a test.
Dim err As System.Windows.Forms.ErrorProvider
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Me.TextBox1.Text = "" Then
err.SetError(Me.TextBox1, "Error1")
Else
' Clear the error, if any, in the error provider.
err.SetError(Me.TextBox1, "")
End If
If Me.TextBox2.Text = "" Then
err.SetError(Me.TextBox2, "Error2")
Else
' Clear the error, if any, in the error provider.
err.SetError(Me.TextBox2, "")
End If
If err.GetError(Me.TextBox1).Length > 0 Or
err.GetError(Me.TextBox2).Length > 0 Then
MsgBox("HAS ERROR")
End If
End Sub

Private Sub Form2_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
err = New System.Windows.Forms.ErrorProvider
End Sub

Please Apply My Suggestion Above And Let Me Know If It Helps Resolve Your
Problem.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Peter,

Well if I have 30 controls on my form, then the ....

If err.GetError(Me.TextBox1).Length > 0 Or _
err.GetError(Me.TextBox2).Length > 0 Or _
err.GetError(Me.TextBox3).Length > 0 Or _
err.GetError(Me.TextBox4).Length > 0 Or _
..
..
err.GetError(Me.TextBox30).Length > 0 Or Then

MsgBox("HAS ERROR")

End If


..... line will be rather long.

Then iw tould be easier with something like...

If err.HasErrors Then
MsgBox("HAS ERROR")
End If

I can derive the class an make my own ErrorProvider, but I was hoping
this "simple" HasError command were build into the ErrorProvider.

Thanks,

M O J O
 
M O J O said:
Well if I have 30 controls on my form, then the ....

If err.GetError(Me.TextBox1).Length > 0 Or _
err.GetError(Me.TextBox2).Length > 0 Or _
err.GetError(Me.TextBox3).Length > 0 Or _
err.GetError(Me.TextBox4).Length > 0 Or _
..
..
err.GetError(Me.TextBox30).Length > 0 Or Then

MsgBox("HAS ERROR")

End If

Why not put the textboxes in an array once, later use a loop?
 
Hi Armin,

Well I created my own ErrorProider like this....



Public Class MojoErrorProvider
Inherits System.Windows.Forms.ErrorProvider

#Region " Component Designer generated code "

Public Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()

'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)
End Sub

Public Sub New()
MyBase.New()

'This call is required by the Component Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Component overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container
End Sub

#End Region

Private _errorList As New ArrayList
Private _activeControl As Control


Public Shadows Sub SetError(ByVal control As Control, ByVal value
As String)
If value = "" Then
If _errorList.Contains(control) Then
_errorList.Remove(control)
End If
Else
If Not _errorList.Contains(_errorList) Then
_errorList.Add(control)
End If
End If
MyBase.SetError(control, value)
End Sub


Public Shadows Sub SetError(ByVal value As String)
If _activeControl Is Nothing Then
Throw New Exception("Active control has not been set.")
End If

Call Me.SetError(_activeControl, value)
End Sub


Public Sub ClearError(ByVal control As Control)
If _errorList.Contains(control) Then
_errorList.Remove(control)
End If

MyBase.SetError(control, "")
End Sub


Public Sub ClearError()
If _activeControl Is Nothing Then
Throw New Exception("Active control has not been set.")
End If

Call Me.ClearError(_activeControl)
End Sub


Public Sub ClearAll()
For Each control As control In _errorList
MyBase.SetError(control, "")
Next
_errorList.Clear()
End Sub


Public Function HasErrors() As Boolean
Return _errorList.Count > 0
End Function

End Class




So now I have the functionality I wanted.

:o)

Thanks for helping me out!

M O J O
 
Hi Mojo,

I am glad you find the solution to achieve your aim.

If you have any concern on this issue,please post here.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top