467,168 Members | 987 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,168 developers. It's quick & easy.

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

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

Nov 21 '05 #1
  • viewed: 9250
Share:
4 Replies
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_Ba...t.htm#download

HTH
Best regards
Ulrich Kulle
***************************************
http://www.help-info.de
***************************************
Nov 21 '05 #2
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

On 4 Mar 2005 10:33:18 -0800, "johnb41" <or****@informatik.com> wrote:
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


Nov 21 '05 #3

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

On Sat, 05 Mar 2005 22:21:04 -0800, J L <jo**@marymonte.com> wrote:
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.
Nov 21 '05 #4
J L
Very clever, Sam! Thanks.

On Mon, 07 Mar 2005 12:21:47 -0500, Samuel R. Neff
<bl****@newsgroup.nospam> wrote:

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

On Sat, 05 Mar 2005 22:21:04 -0800, J L <jo**@marymonte.com> wrote:
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.


Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Martijn Leine | last post: by
6 posts views Thread by M O J O | last post: by
3 posts views Thread by Jeppe Jespersen | last post: by
4 posts views Thread by ljlevend | last post: by
5 posts views Thread by Alvaro Lamas | last post: by
1 post views Thread by johnb41 | last post: by
reply views Thread by chris@soltec.demon.co.uk | last post: by
4 posts views Thread by Freeon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.