VB.NET 迭代控制容器

发布于 2024-12-21 13:32:16 字数 484 浏览 0 评论 0原文

如何循环访问容器内的所有控件以及包含控件的容器中的所有控件,等等。

Form
-Panel
--Control
--Tab
----Control
----Control
--Tab
----Control

以下仅检索 -Panel,而不检索任何其他控件

For Each cntrl As Control In Me.Controls

Next

如何在 For Each 循环中检索所有控件,而无需为堆栈中的每个级别使用 If/Then?

编辑:

Dim ctl As Control = Me
Do
    ctl = Me.GetNextControl(ctl, True)
    'Do whatever you have to ctl
Loop Until ctl Is Nothing

这是迄今为止我发现的最好的方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喵星人汪星人 2024-12-28 13:32:16

您必须定义一个方法来递归遍历容器内的容器。像这样的:

 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

调用这个方法:

 list=new List(Of Control)
  GetChilds(Me)
  For Each cntrl As Control In _list
    ....
  Next

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文