Page 2 of 2 FirstFirst 12
Results 41 to 47 of 47

Thread: Visit Every Control on a Form (includes nested controls, no recursion)

  1. #41
    New Member
    Join Date
    May 2019
    Posts
    15

    Re: Visit Every Control on a Form (includes nested controls, no recursion)

    Close Please
    Last edited by vbfailure; Jun 24th, 2019 at 04:02 AM.

  2. #42

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Visit Every Control on a Form (includes nested controls, no recursion)

    That doesn't look like my code. Also, this is off-topic for this thread, which is about NOT using recursion. CodeBank threads, in particular, should not be cluttered up with tangential subjects. Please create your own thread in the appropriate forum to ask about this issue.

  3. #43
    New Member
    Join Date
    May 2019
    Posts
    15

    Re: Visit Every Control on a Form (includes nested controls, no recursion)

    Close Please
    Last edited by vbfailure; Jun 24th, 2019 at 04:07 AM.

  4. #44
    New Member
    Join Date
    Jul 2021
    Posts
    2

    Re: Visit Every Control on a Form (includes nested controls, no recursion)

    I have made an API Proposal: Add Descendants property for Control for this. I you like it, please upvote it on github.com/dotnet/winforms.

  5. #45

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Visit Every Control on a Form (includes nested controls, no recursion)

    Quote Originally Posted by oliburg View Post
    I have made an API Proposal: Add Descendants property for Control for this. I you like it, please upvote it on github.com/dotnet/winforms.
    I don't really see the point when you can just write an extension method to do it, e.g.
    vb.net Code:
    1. Imports System.Runtime.CompilerServices
    2.  
    3. Public Module ControlExtensions
    4.  
    5.     <Extension>
    6.     Public Iterator Function GetControls(source As Control) As IEnumerable(Of Control)
    7.         Dim ctl As Control = source.GetNextControl(source, True) 'Get the first control in the tab order.
    8.  
    9.         Do Until ctl Is Nothing
    10.             Yield ctl
    11.  
    12.             ctl = source.GetNextControl(ctl, True) 'Get the next control in the tab order.
    13.         Loop
    14.     End Function
    15.  
    16. End Module
    That is implemented using the same principle as the code in post #1 of this thread but you could opt for various other implementations too, e.g.
    vb.net Code:
    1. Imports System.Runtime.CompilerServices
    2.  
    3. Public Module ControlExtensions
    4.  
    5.     <Extension>
    6.     Public Iterator Function GetControls(source As Control, Optional breadthFirst As Boolean = True) As IEnumerable(Of Control)
    7.         If breadthFirst Then
    8.             For Each child As Control In source.Controls
    9.                 Yield child
    10.             Next
    11.  
    12.             For Each child As Control In source.Controls
    13.                 For Each descendent As Control In child.GetControls(breadthFirst)
    14.                     Yield descendent
    15.                 Next
    16.             Next
    17.         Else 'depthFirst
    18.             For Each child As Control In source.Controls
    19.                 For Each descendent As Control In child.GetControls(breadthFirst)
    20.                     Yield descendent
    21.                 Next
    22.  
    23.                 Yield child
    24.             Next
    25.         End If
    26.     End Function
    27.  
    28. End Module

  6. #46
    New Member
    Join Date
    Jul 2021
    Posts
    2

    Re: Visit Every Control on a Form (includes nested controls, no recursion)

    Quote Originally Posted by jmcilhinney View Post
    I don't really see the point when you can just write an extension method to do it, [...]
    Sure you can. That's what I explain in this SO post.
    It would just be handy to have it built-in.

  7. #47
    Addicted Member
    Join Date
    Jan 2022
    Posts
    211

    Re: Visit Every Control on a Form (includes nested controls, no recursion)

    JM, I just wanted to let you know I've used this clever piece of code to thoroughly dispose user control classes in their near entirety. I like your style

    Code:
    If PanelMain.Controls.Count > 0 Then
        Dim UC As UserControl = DirectCast(PanelMain.Controls(0), UserControl)
        Dim ctl As Control = UC.GetNextControl(UC, True)
        Do Until ctl Is Nothing
            ctl.Dispose()
            ctl = UC.GetNextControl(ctl, True)
        Loop
        UC.Dispose()
        PanelMain.Controls.Clear()
    End If

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width