ErrorProvider... how to continue only when there are no errors

J

johnb41

I have a form with a bunch of textboxes. Each text box gets validated
with the ErrorProvider. I want the form to process something ONLY when
all the textboxes are valid.

I found a solution, but it seems like a workaround. I'm not sure if
it's the best way:

Dim ErrorCounter As Integer

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOK.Click
If TextBox1.Text = "" Then
ErrorProvider1.SetError(TextBox1, "Error")
ErrorCounter += 1
Else
ErrorProvider1.SetError(TextBox1, "")
End If

If TextBox2.Text = "" Then
ErrorProvider1.SetError(TextBox2, "Error2")
ErrorCounter += 1
Else
ErrorProvider1.SetError(TextBox2, "")
End If

If ErrorCounter = 0 Then
'process code that only happens when there are NO errors
End If
End Sub


Is this a good way to handle it, or am I missing a better technique?

Thanks!
John
 
U

Ulrich Kulle

Hello John,
I have a form with a bunch of textboxes. Each text box gets validated
with the ErrorProvider. I want the form to process something ONLY when
all the textboxes are valid.

I found a solution, but it seems like a workaround. I'm not sure if
it's the best way:

Dim ErrorCounter As Integer

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOK.Click
If TextBox1.Text = "" Then
ErrorProvider1.SetError(TextBox1, "Error")
ErrorCounter += 1
Else
ErrorProvider1.SetError(TextBox1, "")
End If

If TextBox2.Text = "" Then
ErrorProvider1.SetError(TextBox2, "Error2")
ErrorCounter += 1
Else
ErrorProvider1.SetError(TextBox2, "")
End If

If ErrorCounter = 0 Then
'process code that only happens when there are NO errors
End If
End Sub


Is this a good way to handle it, or am I missing a better technique?

I have no better idea _ seems this is OK.

If you are interested have a look at:
http://www.help-info.de/en/Visual_Basic_net/vbnet.htm#download

HTH
Best regards
Ulrich Kulle
***************************************
http://www.help-info.de
***************************************
 
J

J L

You dont have to use a counter. You can step through the error
provider to see if any errors are set:

Dim ctrl As Control
Dim strErrorList As Stromg

strErrorList = ""
For Each ctrl In Me.Controls
If Len(ErrorProvider1.GetError(ctrl)) > 0 Then
strErrorList += ErrorProvider1.GetError(ctrl) & ChrW(10) &
ChrW(13)
End If
Next

If Len(strErrorList) = 0 Then
' Process stuff if no errors
Else
Messagebox.Show(strErrorList, "List Of Errors")
End If

John
 
S

Samuel R. Neff

Also have to watch out if you're using a groupbox or other container
since those controls are not in Me.Controls but may be in the error
provider. I use a recursive routine to check..

Public Shared Function ErrorProviderErrorsList(ByVal provider As
ErrorProvider) As String()
Dim errors As New ArrayList
ErrorProviderErrorsList(provider,
provider.ContainerControl.Controls, errors)
Return DirectCast(errors.ToArray(GetType(String)), String())
End Function

Private Shared Sub ErrorProviderErrorsList(ByVal provider As
ErrorProvider, ByVal controls As Control.ControlCollection, ByVal
errors As ArrayList)
Dim s As String
For Each ctl As Control In controls
s = provider.GetError(ctl)
If s.Length > 0 Then
errors.Add(s)
End If

ErrorProviderErrorsList(provider, ctl.Controls, errors)
Next
End Sub


HTH,

Sam



You dont have to use a counter. You can step through the error
provider to see if any errors are set:

Dim ctrl As Control
Dim strErrorList As Stromg

strErrorList = ""
For Each ctrl In Me.Controls
If Len(ErrorProvider1.GetError(ctrl)) > 0 Then
strErrorList += ErrorProvider1.GetError(ctrl) & ChrW(10) &
ChrW(13)
End If
Next

If Len(strErrorList) = 0 Then
' Process stuff if no errors
Else
Messagebox.Show(strErrorList, "List Of Errors")
End If

John
B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
 
J

J L

Very clever, Sam! Thanks.

Also have to watch out if you're using a groupbox or other container
since those controls are not in Me.Controls but may be in the error
provider. I use a recursive routine to check..

Public Shared Function ErrorProviderErrorsList(ByVal provider As
ErrorProvider) As String()
Dim errors As New ArrayList
ErrorProviderErrorsList(provider,
provider.ContainerControl.Controls, errors)
Return DirectCast(errors.ToArray(GetType(String)), String())
End Function

Private Shared Sub ErrorProviderErrorsList(ByVal provider As
ErrorProvider, ByVal controls As Control.ControlCollection, ByVal
errors As ArrayList)
Dim s As String
For Each ctl As Control In controls
s = provider.GetError(ctl)
If s.Length > 0 Then
errors.Add(s)
End If

ErrorProviderErrorsList(provider, ctl.Controls, errors)
Next
End Sub


HTH,

Sam




B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top