0

On a VB.NET project with Metroframework Modern UI 1.4.0.0 installed, I use this part of code into a Module to loop over all MetroTextBoxes which placed into a MetroTabControl and give Cursor.Hand to their embedded Clear button. How can I do the same thing including MetroTextBoxes which could be out of MetroTabControl, into my form or into an other container for example? In other words, I would like to loop over every MetroTextBox into a form, even if it is nested.

Public Sub TxtBoxes_Cursors(sender, e)
    Dim _FormSender = DirectCast(sender, MetroFramework.Forms.MetroForm)
    For Each _MetroTabControl As Control In _FormSender.Controls.OfType(Of MetroFramework.Controls.MetroTabControl)()
        For Each _TabPage As Control In _MetroTabControl.Controls.OfType(Of MetroFramework.Controls.MetroTabPage)()
            For Each _Textbox As Control In _TabPage.Controls.OfType(Of MetroFramework.Controls.MetroTextBox)()
                If _Textbox.HasChildren Then
                    For Each _ClearButton As Control In _Textbox.Controls
                        If _ClearButton.Name = "lnkClear" Then
                            _ClearButton.Cursor = System.Windows.Forms.Cursors.Hand
                        End If
                    Next
                End If
            Next
        Next
    Next
End Sub
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
Simos Sigma
  • 958
  • 7
  • 29
  • 1
    The word is 'Nested' :) – A Friend Feb 28 '18 at 09:57
  • 3
    Possible duplicate of [loop over all textboxes in a form, including those inside a groupbox](https://stackoverflow.com/questions/4673950/loop-over-all-textboxes-in-a-form-including-those-inside-a-groupbox) – A Friend Feb 28 '18 at 09:58
  • @AFriend: Thank you very much for your help my friend!!! :) – Simos Sigma Feb 28 '18 at 10:38
  • Programming language and related technology is indicated by the question's tags. Please do not put them in the title unless they fulfill a specific purpose. Refer to: [Should questions include "tags" in their titles?](https://meta.stackexchange.com/q/19190) – Visual Vincent Feb 28 '18 at 15:02
  • @Visual Vincent: Sorry!!! – Simos Sigma Feb 28 '18 at 22:56

1 Answers1

1

This is the exact answer to my question according to the accepted answer of this relevant question which user A Friend pointed on comments. First I have to place this Function into my Module:

Public 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

And then edit my Public Sub like this:

Public Sub TxtBoxes_Cursors(sender, e)
    Dim _FormSender = DirectCast(sender, MetroFramework.Forms.MetroForm)
    Dim _MetroTextBoxes As New List(Of Control)
    For Each _Textbox As MetroFramework.Controls.MetroTextBox In FindControlRecursive(_MetroTextBoxes, _FormSender, GetType(MetroFramework.Controls.MetroTextBox))
        If _Textbox.HasChildren Then
            For Each _ClearButton As Control In _Textbox.Controls
                If _ClearButton.Name = "lnkClear" Then
                    _ClearButton.Cursor = System.Windows.Forms.Cursors.Hand
                End If
            Next
        End If
    Next
End Sub
Simos Sigma
  • 958
  • 7
  • 29