I'm fairly new at this, so please bear with me. I am trying to reset the background of text boxes that are located within different groupboxes. The code I am using is as follows:
Private Function ResetBackGround(ByVal form As System.Object)
For Each ctrl As Control In form.Controls
If ctrl.GetType.Name = "TextBox" Then
ctrl.BackColor = Color.White
End If
Next
End Function
Called by:
ResetBackGround(Form1.ActiveForm)
The function will reset any textbox on the form not part of a groupbox. I've tried various code variations in an attempt to access the textboxes in the groups, but no luck. Any help would be greatly appreciated.
Thanks,
Bret

Accessing Controls Located in a GroupBox
miznig
Private Sub ResetAllBackgrounds(ByVal ctl As Control)
If ctl.GetType Is GetType(TextBox) Then
ctl.BackColor = Color.White
End If
For Each c As Control In ctl.Controls
If c.GetType Is GetType(TextBox) Then
c.BackColor = Color.White
End If
ResetAllBackgrounds(c)
Next
End Sub
Then call this sub in your loop:
Private Function ResetBackGround(ByVal form As System.Object)
For Each ctrl As Control In form.Controls
ResetAllBackgrounds(ctrl)
Next
End Function
Note that your function should really be a subroutine since it does not return a value.
HTH
NeXuS_ITA
I'm curious though why a recursive sub had to be used. Why wouldn't my original method of using the "For Each ctrl" statement work
Thanks again,
BDS
w1z44rdy
Using recursion allows the method to test each control in a given controls collection and then test the controls within <i>that</i> control's controls collection.
Think of it like looking for folders on your hard drive. If passed the folder "C:\Windows", your loop would only find the folders directly under c:\windows like c:\windows\system and c:\windows\system32. It would not find the subfolders within those folders; such as c:\windows\system32\Microsoft. But using recursion, the sub looks for all folders within the given folder "c:\windows" and then calls itself, passing each folder it finds in c:\windows. This contiunes in turn until no folders are found. The current loop then ends and the routine starts over with the next element of the previous level.
Make sense