'______________________________________________________________ ' C O N T R O L S C O L L E C T I O N D E M O ' Ron Kessler ' Created 9/24/08 '______________________________________________________________ 'This demo shows how to cycle through the controls collection on a form to clear all testboxes. 'However, notice there is a textbox inside of a container control (Groupbox). The form's control 'collection will not see this textbox because it is inside a container control. So we will use the 'HasChildren property of the controls in the form's collection to find the nested controls! Public Class frmMain Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click '---cycle through forms collection and clear textboxes For Each myControl As Control In Me.Controls If TypeOf myControl Is TextBox Then myControl.Text = "" ElseIf myControl.HasChildren Then '---if it comes across a groupbox or tab control that has child controls, clear textboxes & radiobuttons in them also For Each nestedControl As Control In myControl.Controls If TypeOf nestedControl Is TextBox Then nestedControl.Text = "" ElseIf TypeOf nestedControl Is RadioButton Then Dim myRadioButton As RadioButton = nestedControl 'see note below myRadioButton.Checked = False End If Next End If Next End Sub 'Note about the RadioButton loop above: 'All controls have a text property (I think). But not all controls have a Checked property. So to 'uncheck a RadioButton or Checkbox Control, I have to create a new reference variable (myRadioButton) line 31 'and assign it to the current control being pointed to in nestedControl. Then I can access its checked 'property and uncheck it by making it false in line 32. End Class