By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,822 Members | 729 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,822 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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.