Connecting Tech Pros Worldwide Forums | Help | Site Map

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

johnb41
Guest
 
Posts: n/a
#1: Nov 21 '05
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

Ulrich Kulle
Guest
 
Posts: n/a
#2: Nov 21 '05

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


Hello John,
[color=blue]
> 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?[/color]

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
***************************************


J L
Guest
 
Posts: n/a
#3: Nov 21 '05

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


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" <orders@informatik.com> wrote:
[color=blue]
>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[/color]

Samuel R. Neff
Guest
 
Posts: n/a
#4: Nov 21 '05

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



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 <john@marymonte.com> wrote:
[color=blue]
>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
>[/color]
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 L
Guest
 
Posts: n/a
#5: Nov 21 '05

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


Very clever, Sam! Thanks.

On Mon, 07 Mar 2005 12:21:47 -0500, Samuel R. Neff
<blinex@newsgroup.nospam> wrote:
[color=blue]
>
>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 <john@marymonte.com> wrote:
>[color=green]
>>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
>>[/color]
>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.[/color]

Closed Thread


Similar Visual Basic .NET bytes