Reputation: 806
How do I go about looping through all the controls within a container, and all the controls in the container of a containing control, and so on.
Form
-Panel
--Control
--Tab
----Control
----Control
--Tab
----Control
The following only retrieves -Panel and none of the other controls
For Each cntrl As Control In Me.Controls
Next
How can I retrieve them all in a For Each loop without an If/Then for every level in the stack?
EDIT:
Dim ctl As Control = Me
Do
ctl = Me.GetNextControl(ctl, True)
'Do whatever you have to ctl
Loop Until ctl Is Nothing
This is so far the best method I found of doing this.
Upvotes: 0
Views: 4613
Reputation: 94645
You have to define a method that recursively
traverse containers inside the container. Something like this:
Dim _list As New List(Of Control)
Public Sub GetChilds(container As Control)
For Each child As Control In container.Controls
_list.Add(child)
If (child.HasChildren) Then
GetChilds(child)
End If
Next
End Sub
To call this method:
list=new List(Of Control)
GetChilds(Me)
For Each cntrl As Control In _list
....
Next
Upvotes: 2