计算winform中的特定控件出现次数

时间:2013-04-08 02:06:40

标签: vb.net winforms visual-studio-2010 tabs textbox

winform包含放置在面板中的多个文本框,面板放置在选项卡控件的选项卡页面中。所以,我想要的是,我可以计算该winform中文本框控件的数量,以及如何访问所有文本框。

欢迎提出建议。

1 个答案:

答案 0 :(得分:2)

这是一段快速而肮脏的代码,演示了如何递归遍历表单上的所有控件。它是递归的,这意味着它将通过其他容器控件进行挖掘。

    ' create a list of textboxes
    Dim allTextBoxes As New List(Of TextBox)

    ' call a recursive finction to get a list of all the textboxes
    ExamineControls(allTextBoxes, Me.Controls)

    ' run through the list and look at them
    For Each t As TextBox In allTextBoxes
        Debug.Print(t.Name)
    Next



Private Sub ExamineControls(allTextBoxes As List(Of TextBox), controlCollection As Control.ControlCollection)
    For Each c As Control In controlCollection
        If TypeOf c Is TextBox Then
            ' it's a textbox, add it to the collection
            allTextBoxes.Add(c)
        ElseIf c.Controls IsNot Nothing AndAlso c.Controls.Count > 0 Then
            ' it's some kind of container, recurse
            ExamineControls(allTextBoxes, c.Controls)
        End If
    Next
End Sub

您显然希望将此逻辑移动到表单的不同部分,并将结果存储在文本框类型的表单级列表中...