< /p>
For Each c As Control In Me.Controls
If c.GetType Is GetType(TextBox) Then
'Do something
End If
Next
But it seems to skip those in the groupbox and only loop to the other text boxes of the form. So I added another For Each loop to the groupbox text box:
< pre>For Each c As Control In GroupBox1.ControlsIf c.GetType Is GetType(TextBox) Then
‘Do something
End If
Next
I want to know: Is there a way to loop through all the text boxes in the form-including the text boxes in the group box-with only one For Each loop? Or a better/more elegant way to do this?
Thanks in advance.
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
< p>I have several text boxes in winform, some of them are in groupbox. I am trying to loop through all text boxes 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 seems to skip Those in the groupbox and Only loop to the other text boxes of the form. So I added another For Each loop to the groupbox text box:
For Each c As Control In GroupBox1.Controls
If c.GetType Is GetType(TextBox) Then
'Do something
End If
Next
I want to know: Is there a way to loop through all the text boxes in the form – Including the text box in the group box – Only one For Each loop? Or a better/more elegant way to do this?
Thanks in advance.
You can use this feature, linq may 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