loop over all textboxes in a form, including those inside a groupbox

后端 未结 4 1289
执笔经年
执笔经年 2020-11-27 20:16

I have several textboxes in a winform, some of them are inside a groupbox. I tried to loop over all textboxes in my form:

For Each c As Control In Me.Controls         


        
4条回答
  •  再見小時候
    2020-11-27 21:05

    You can use this function, linq might be a more elegant way.

    Dim allTxt As New List(Of Control)
    For Each txt As TextBox In FindControlRecursive(allTxt, Me, GetType(TextBox))
       '....'
    Next
    
    Public Shared Function FindControlRecursive(ByVal list As List(Of Control), ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control)
        If parent Is Nothing Then Return list
        If parent.GetType Is ctrlType Then
            list.Add(parent)
        End If
        For Each child As Control In parent.Controls
            FindControlRecursive(list, child, ctrlType)
        Next
        Return list
    End Function
    

提交回复
热议问题