-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 に答える