【问题标题】:VB.Net List of ControlsVB.Net 控件列表
【发布时间】:2015-02-03 00:01:28
【问题描述】:

我想获取所有控件的列表;我设法在下面的代码中做到了这一点;问题是这确实只列出了容器外的控件。我想获取所有控件的列表,包括容器内的控件(例如 TabControl 内的控件)。我的问题是:如何在 VB.Net 中做到这一点?

Sub ListAllControls()
    Me.RichTextBox1.Clear()
    Dim MekdamCTL As Control
    For Each MekdamCTL In Me.Controls
        Me.RichTextBox1.AppendText("Control: " & MekdamCTL.Name & Environment.NewLine)
    Next
    Me.RichTextBox1.AppendText("How to include ALL Controls in ALL Containers, such as Controls in TabControl ??")
End Sub

提前感谢您的帮助和/或 cmets。

【问题讨论】:

  • 您可以使用递归,但不是必须的。看我的回答。

标签: vb.net visual-studio-2012


【解决方案1】:

您需要一个可以从主窗体调用的递归函数

Public Sub ListControls(coll As Control.ControlCollection)
    for each ctr in coll
       Me.RichTextBox1.AppendText("Control: " & ctr.Name & Environment.NewLine)
       if ctr.Controls.Count > 0 Then
          ListControls(ctr.Controls)
       End if
    Next
End Sub

从主窗体调用它

ListControls(Me.Controls)

【讨论】:

  • 我得到了答案;但我只是好奇,这段代码没有列出“内部控件”??你能检查一下吗?它给出了与我在问题中发布的代码相同的结果。
  • 好吧,不打印控件容器的名字,现在修好了。
  • 递归方法有效,但您不需要递归。
  • @dbasnett 你是对的。只是遵循预先设定的思路,而不是开箱即用。支持您的答案,因为它是最好的
【解决方案2】:

试试这个:

    'this loop will get all the controls on the form
    'no matter what the level of container nesting

    Dim ctrl As Control = Me.GetNextControl(Me, True)
    Do Until ctrl Is Nothing
        RichTextBox1.AppendText("Control: " & ctrl.Name & Environment.NewLine)
        ctrl = Me.GetNextControl(ctrl, True)
    Loop

使用此方法,无需检查或递归。

【讨论】:

    【解决方案3】:

    这比您需要的要复杂一些,但以下函数返回任何给定类型的所有控件,并可选择搜索任何嵌入式容器

    ''' <summary>Get all Controls of a specified type within a container and optionally any embedded containers</summary>
    ''' <typeparam name="ctrlType">The type of Control to be searched for (must inherit from Control)</typeparam>
    ''' <param name="parent">The top level container</param>
    ''' <param name="searchContainers">True if embedded containers are to be searched</param>
    ''' <returns>IEnumerable with all Controls of the specified type that were found</returns>
    Function GetControlsOfType(Of ctrlType As Control)(parent as Control, searchContainers As Boolean) As IEnumerable(Of ctrlType)
        Dim ctrls As New List(Of ctrlType)
        For Each ctrl As Control In parent.Controls 
            If TypeOf ctrl Is ctrlType Then ctrls.Add(DirectCast(ctrl, ctrlType))
            If searchContainers AndAlso ctrl.Controls.Count > 0 Then ctrls.AddRange(GetControlsOfType(Of ctrlType)(ctrl, searchContainers))
        Next
        Return ctrls
    End Function
    

    如果要列出任何类型的所有控件,可以这样调用:

    For Each ctrl As Control In GetControlsOfType(Of Control)(Me, True)
        Me.RichTextBox1.AppendText("Control: " & ctrl.Name & vbCrLf)
    Next
    

    【讨论】:

      【解决方案4】:

      这个可以用吗:

      Private Sub frmWRMenu_Activated(sender As Object, e As EventArgs) Handles Me.Activated
      
      Dim k As New List(Of String)
           Dim myNumber = 0
           Dim myCtrl As Control = Me
      Repeat:
          For a = 1 To myCtrl.Controls.Count
              k.Add(myCtrl.Controls(a - 1).Name)
              Debug.Print(myCtrl.Controls(a - 1).Name)
          Next
          If (myNumber + 1) <= k.Count Then
              myCtrl = Me.Controls.Find(k(myNumber ), True)(0)
              myNumber += 1
              GoTo Repeat
          End If
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2017-08-19
        • 1970-01-01
        • 2011-01-24
        • 1970-01-01
        • 1970-01-01
        • 2012-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多