[英]VB.NET Iterating through control containers
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 以下仅检索-Panel而不检索其他控件
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? 如何在For Each循环中检索它们,而不为堆栈中的每个级别使用If / Then?
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. 这是迄今为止我发现的最好的方法。
You have to define a method that recursively
traverse containers inside the container. 您必须定义一个
recursively
遍历容器内容器的方法。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.