none
Select all control names in dynamically RRS feed

  • Question

  • HI,

     I need get all control names in dynamically 

     Button , Label , Textbox , Combobox , Checkbox and Contextmenustrip items etc...

    

    Please help

    Saturday, September 29, 2018 6:39 AM

Answers

  • You can do something like :

    Function GetAllControls(Of TControl As Control)(source As Control) As IEnumerable(Of TControl)
        Dim controls = source.Controls.Cast(Of Control)()
        Return controls.SelectMany(Function(child) GetAllControls(Of TControl)(child)).OfType(Of TControl).Concat(controls.OfType(Of TControl))
    End Function

    Then :

    Dim controls = GetAllControls(Of Control)(Me)
    Array.ForEach(controls.ToArray, Sub(control) Console.WriteLine("Control : {0} ", control.Name))

    • Marked as answer by Farook M Wednesday, October 3, 2018 6:09 AM
    Saturday, September 29, 2018 9:31 AM
  • Seems I was a few minutes late as this is the same answer. But note, if the control name is empty e.g. HSCrollbar then there will be no name for the control.

    ''' <summary>
    ''' Get all controls in container
    ''' </summary>
    ''' <param name="list">List of Control newed up before calling</param>
    ''' <param name="container">e.g. Me, GroupBox1 etc</param>
    Private Sub AddChildControls(list As List(Of Control), container As Control)
        For Each ctl As Control In container.Controls
            list.Add(ctl)
            If ctl.Controls.Count > 0 Then
                AddChildControls(list, ctl)
            End If
        Next
    End Sub

    Usage

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim list = New List(Of Control)()
        AddChildControls(list, Me)
        If list.Count > 0 Then
            Console.WriteLine()
            list.ForEach(Sub(c) Console.WriteLine($"Name: {If(String.IsNullOrWhiteSpace(c.Name), "None", c.Name),20} Type: {c.GetType()}"))
        End If
    End Sub

    We can also focus on specific controls.

    Public Module Extensions
        <Runtime.CompilerServices.Extension()>
        Public Function AllControls(ByVal container As Control) As IEnumerable(Of Control)
            Dim list = New List(Of Control)()
            AddChildControls(list, container)
            Return list
        End Function
        ''' <summary>
        ''' Get all controls in container
        ''' </summary>
        ''' <param name="list">List of Control newed up before calling</param>
        ''' <param name="container">e.g. Me, GroupBox1 etc</param>
        Public Sub AddChildControls(list As List(Of Control), container As Control)
            For Each ctl As Control In container.Controls
                list.Add(ctl)
                If ctl.Controls.Count > 0 Then
                    AddChildControls(list, ctl)
                End If
            Next
        End Sub
    
    End Module

    Usage

    Dim buttonList = From C In Me.AllControls.OfType(Of Button)() Select C

    Or we can do

    Public Module Extensions
        <Runtime.CompilerServices.Extension()>
        Public Function GetAll(ByVal control As Control) As List(Of Control)
            Dim controls = control.Controls.Cast(Of Control)()
            Return controls.SelectMany(Function(ctrl) ctrl.GetAll().Concat(controls)).ToList()
        End Function
    End Module
    

    Then

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        GetAll().ForEach(
            Sub(c)
                Console.WriteLine($"Name: {If(String.IsNullOrWhiteSpace(c.Name), "None", c.Name),20} Type: {c.GetType()}")
            End Sub)
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Saturday, September 29, 2018 9:39 AM
    Moderator

All replies

  • You can do something like :

    Function GetAllControls(Of TControl As Control)(source As Control) As IEnumerable(Of TControl)
        Dim controls = source.Controls.Cast(Of Control)()
        Return controls.SelectMany(Function(child) GetAllControls(Of TControl)(child)).OfType(Of TControl).Concat(controls.OfType(Of TControl))
    End Function

    Then :

    Dim controls = GetAllControls(Of Control)(Me)
    Array.ForEach(controls.ToArray, Sub(control) Console.WriteLine("Control : {0} ", control.Name))

    • Marked as answer by Farook M Wednesday, October 3, 2018 6:09 AM
    Saturday, September 29, 2018 9:31 AM
  • Seems I was a few minutes late as this is the same answer. But note, if the control name is empty e.g. HSCrollbar then there will be no name for the control.

    ''' <summary>
    ''' Get all controls in container
    ''' </summary>
    ''' <param name="list">List of Control newed up before calling</param>
    ''' <param name="container">e.g. Me, GroupBox1 etc</param>
    Private Sub AddChildControls(list As List(Of Control), container As Control)
        For Each ctl As Control In container.Controls
            list.Add(ctl)
            If ctl.Controls.Count > 0 Then
                AddChildControls(list, ctl)
            End If
        Next
    End Sub

    Usage

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim list = New List(Of Control)()
        AddChildControls(list, Me)
        If list.Count > 0 Then
            Console.WriteLine()
            list.ForEach(Sub(c) Console.WriteLine($"Name: {If(String.IsNullOrWhiteSpace(c.Name), "None", c.Name),20} Type: {c.GetType()}"))
        End If
    End Sub

    We can also focus on specific controls.

    Public Module Extensions
        <Runtime.CompilerServices.Extension()>
        Public Function AllControls(ByVal container As Control) As IEnumerable(Of Control)
            Dim list = New List(Of Control)()
            AddChildControls(list, container)
            Return list
        End Function
        ''' <summary>
        ''' Get all controls in container
        ''' </summary>
        ''' <param name="list">List of Control newed up before calling</param>
        ''' <param name="container">e.g. Me, GroupBox1 etc</param>
        Public Sub AddChildControls(list As List(Of Control), container As Control)
            For Each ctl As Control In container.Controls
                list.Add(ctl)
                If ctl.Controls.Count > 0 Then
                    AddChildControls(list, ctl)
                End If
            Next
        End Sub
    
    End Module

    Usage

    Dim buttonList = From C In Me.AllControls.OfType(Of Button)() Select C

    Or we can do

    Public Module Extensions
        <Runtime.CompilerServices.Extension()>
        Public Function GetAll(ByVal control As Control) As List(Of Control)
            Dim controls = control.Controls.Cast(Of Control)()
            Return controls.SelectMany(Function(ctrl) ctrl.GetAll().Concat(controls)).ToList()
        End Function
    End Module
    

    Then

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        GetAll().ForEach(
            Sub(c)
                Console.WriteLine($"Name: {If(String.IsNullOrWhiteSpace(c.Name), "None", c.Name),20} Type: {c.GetType()}")
            End Sub)
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Saturday, September 29, 2018 9:39 AM
    Moderator
  • Hi,

    try the code:

     Public Function GetAllControls(ByVal control As Control) As List(Of Control)
            Dim list = New List(Of Control)()
    
            For Each con As Control In control.Controls
                list.Add(con)
    
                If con.Controls.Count > 0 Then
                    list.AddRange(GetAllControls(con))
                End If
            Next
    
            Return list
        End Function

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Saturday, September 29, 2018 12:40 PM
  • Hi Castorix,

    Working fine, but ContextMenuStrip items name not coming

    pls help

    Wednesday, October 3, 2018 9:10 AM