-2

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

欢迎提出建议。

4

1 回答 1

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

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

于 2013-04-08T03:18:45.343 回答