【问题标题】:How to access controls of Multilayer Panels in vb.net如何在 vb.net 中访问多层面板的控件
【发布时间】:2013-06-12 10:29:12
【问题描述】:

我想使用以下代码访问表单中的所有控件:

对于 Myform.control 中的每台电脑

做某事

我的问题是我的表单中有多层面板。例如“Myform”包含 (textbox1,textbox 2,combobox1,panle1,panel2)。

Panel1 包含(panel11 和文本框 3)

面板 2 包含(panel22 和 textbox4 和 combobox2)

另外panel22包含(textbox5和panle222)

如何在不考虑是否在面板中的情况下访问“Myform”中的“所有”控件(文本框和组合框)。

非常感谢任何帮助。

【问题讨论】:

  • 我能问你为什么需要这个吗?
  • 我想动态地将值插入文本框和组合框。

标签: vb.net controls panel multi-layer


【解决方案1】:

应该这样做:

Private Sub EnumerateControl(parentControl As Control)
    For Each child As Control In parentControl.Controls
        Debug.WriteLine(child.Name)
        If child.HasChildren Then EnumerateControl(child)
    Next
End Sub

然后调用它来使用它:

EnumerateControl(Me) 'Pass the form control to start the enumeration

这里的关键是测试相关控件是否有子控件,如果有,则通过递归调用EnumerateControl 枚举该控件中的所有控件

【讨论】:

  • 为什么要通过 ByRef?这对这种方法应该没有任何影响。
【解决方案2】:

您可以通过递归方式访问它们,例如:

Public Sub ProcessControls(ByRef Controls As ControlCollection)
    For Each pc As Control In Controls
        'Do whathever you want

        If pc.Controls.Count Then 'If that control has child, process them
            ProcessControls(pc.Controls)
        End If
    Next
End Sub

【讨论】:

    猜你喜欢
    • 2010-11-18
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多