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

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
    If c.GetType Is GetType(TextBox) Then
        ' Do something
    End If
Next

But it seemed to skip those inside the groupbox and loop only over the other textboxes of the form. So I added another For Each loop for the groupbox textboxes:

For Each c As Control In GroupBox1.Controls
    If c.GetType Is GetType(TextBox) Then
        ' Do something
    End If
Next

I wonder: is there a way to loop over all textboxes in a form--including those inside a groupbox--with a single For Each loop? Or any better/more elegant way to do it?


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

You'd want to do recursion, for example (pseudocode, since I do not know VB):

Sub LoopControls (Control Container)
    if (Container has Controls)
        LoopControls(Container)
    For Each c As Control In Container
        if (Control is TextBox)
            // do stuff
End Sub

You would pass your form (me) to the sub initially, and it will traverse the controls in it looking for ones that contain more controls.

Also check out this question: VB.NET - Iterating through controls in a container object


If you don't care about the order (and I can't imagine a reason why you should) of the controls, you can do this iteratively in the following fashion (its quite straightforward, so I don't think any explanation is necessary). Should be much more efficient than any recursion, especially if you have many nested controls, though I doubt if the performance gain would be apparent.

Public Function FindAllControlsIterative(ByRef parent As Control) As List(Of TextBox)
    Dim list As New List(Of TextBox)
    Dim ContainerStack As New Stack(Of Control)
    ContainerStack.Push(parent)
    While ContainerStack.Count > 0
        For Each child As Control In ContainerStack.Pop().Controls
            If child.HasChildren Then ContainerStack.Push(child)
            If child.GetType Is GetType(TextBox) Then list.Add(child)
        Next
    End While
    Return list
End Function