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

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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,473

    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,473

    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.

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