Checking Controls for errors

Z

Zim Babwe

I am using the following to see if an error exists on any of my controls on
my Windows Form (VS 2005)

' Check to see if errors exist
'
For Each ctrl As Control In Me.Controls
If ErrorProvider1.GetError(ctrl) <> "" Then
invalidInput = True
End If
Next

I have three group controls on my form and in each group control, there are
4 textboxes. The above code checks the group control but bypasses the
textboxes.

How can I include the textboxes?

Thanks in advance
 
R

RobinS

Zim Babwe said:
I am using the following to see if an error exists on any of my controls
on my Windows Form (VS 2005)

' Check to see if errors exist
'
For Each ctrl As Control In Me.Controls
If ErrorProvider1.GetError(ctrl) <> "" Then
invalidInput = True
End If
Next

I have three group controls on my form and in each group control, there
are 4 textboxes. The above code checks the group control but bypasses
the textboxes.

How can I include the textboxes?

Thanks in advance

You can use a recursive routine to access all the controls. Your loop hits
the GroupControl and doesn't look *inside* it.

Or you can specifically check the textboxes inside the GroupControl.

Here's how you could do it recursively. I didn't test this, but it should
work (famous last words). Call it and pass it the form, like this:

CheckForErrors(myForm)
if invalidInput Then
...
End If

'This needs to be outside the routine.
Dim invalidInput As Boolean

Private Sub CheckForErrors(ByVal ctrlContainer As Control)
For Each ctrl As Control In ctrlContainer.Controls
If TypeOf ctrl Is TextBox _
AndAlso ErrorProvider1.GetError(ctrl) <> String.Empty Then
invalidInput = True
Return
End If
'If control has children, call this function recursively
If ctrl.HasChildren Then
CheckForErrors(ctrl)
'If it found any invalid inputs in this container, return.
If invalidInput Then Return
End If
Next
End Sub

Good luck.
Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
 
Z

Zim Babwe

Thanks. This is just what I need.


RobinS said:
You can use a recursive routine to access all the controls. Your loop hits
the GroupControl and doesn't look *inside* it.

Or you can specifically check the textboxes inside the GroupControl.

Here's how you could do it recursively. I didn't test this, but it should
work (famous last words). Call it and pass it the form, like this:

CheckForErrors(myForm)
if invalidInput Then
...
End If

'This needs to be outside the routine.
Dim invalidInput As Boolean

Private Sub CheckForErrors(ByVal ctrlContainer As Control)
For Each ctrl As Control In ctrlContainer.Controls
If TypeOf ctrl Is TextBox _
AndAlso ErrorProvider1.GetError(ctrl) <> String.Empty Then
invalidInput = True
Return
End If
'If control has children, call this function recursively
If ctrl.HasChildren Then
CheckForErrors(ctrl)
'If it found any invalid inputs in this container, return.
If invalidInput Then Return
End If
Next
End Sub

Good luck.
Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
 

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