You *could* store them in a collection:
Dim theControls as ControlCollection = Me.Controls
but it only stores them in the array. To go through each control, you would need to iterate through each control
William Bartholomew wrote:
Each container control (including forms) have a Controls property which returns a collection of the child controls of the container. You would need to loop through this recursively to get all of the controls.
tnx, but can you give me a sample code. i have a hard time doing it. i need to get the controls inside the controls too.
but you stated you didnt want to use a for each loop to go through the controls? There is no other way of doing so in this case....apart from using the for each loop
for each currentControl as Control in Me.Controls
if currentControl.GetType Is GetType(SomeControl) then
Dim theControl as SomeControl = CType(currentControl, SomeControl)
'do whatever with the current control
end if
next
replace "SomeControl" with the control you are after, such as TextBox or Button or Label....
Dim allControls As New Collection(Of Control)
EnumerateControls(Me, allControls)
Private Sub EnumerateControls( ByVal container As Control, ByVal controlCollection As Collection( Of Controls ) )
For Each currentControl In container.Controls
controlCollection.Add( currentControl )
EnumerateControls( currentControl, controlCollection )
Next
End Sub
thanks guys. here's what i got from your ideas
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each ctl As Control In Me.Controls
MsgBox(ctl.Name, MsgBoxStyle.Information, "Root")
ec(ctl, 1)
Next
End SubPrivate Sub ec(ByVal mctl As Control, ByVal i As Integer)For Each xctl As Control In mctl.ControlsSelect Case TypeName(xctl)Case "Tabcontrol"ec(xctl, i)
Case "TabPage"ec(xctl, i)
i = i + 1
Case "Panel", "TableLayoutPanel", "GroupBox"ec(xctl, i)
i = i + 1
Case "Label", "Button", "RadioButton", "CheckBox"i = i + 1
End SelectMsgBox(xctl.Name, MsgBoxStyle.Information,
"level " & i)NextEnd Sub