-
Jun 20th, 2019, 07:05 AM
#41
New Member
Re: Visit Every Control on a Form (includes nested controls, no recursion)
Last edited by vbfailure; Jun 24th, 2019 at 04:02 AM.
-
Jun 20th, 2019, 09:00 AM
#42
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.
-
Jun 20th, 2019, 09:54 AM
#43
New Member
Re: Visit Every Control on a Form (includes nested controls, no recursion)
Last edited by vbfailure; Jun 24th, 2019 at 04:07 AM.
-
Jul 5th, 2021, 08:57 AM
#44
New Member
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.
-
Jul 5th, 2021, 10:13 AM
#45
Re: Visit Every Control on a Form (includes nested controls, no recursion)
Originally Posted by oliburg
I don't really see the point when you can just write an extension method to do it, e.g.
vb.net Code:
Imports System.Runtime.CompilerServices
Public Module ControlExtensions
<Extension>
Public Iterator Function GetControls(source As Control) As IEnumerable(Of Control)
Dim ctl As Control = source.GetNextControl(source, True) 'Get the first control in the tab order.
Do Until ctl Is Nothing
Yield ctl
ctl = source.GetNextControl(ctl, True) 'Get the next control in the tab order.
Loop
End Function
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:
Imports System.Runtime.CompilerServices
Public Module ControlExtensions
<Extension>
Public Iterator Function GetControls(source As Control, Optional breadthFirst As Boolean = True) As IEnumerable(Of Control)
If breadthFirst Then
For Each child As Control In source.Controls
Yield child
Next
For Each child As Control In source.Controls
For Each descendent As Control In child.GetControls(breadthFirst)
Yield descendent
Next
Next
Else 'depthFirst
For Each child As Control In source.Controls
For Each descendent As Control In child.GetControls(breadthFirst)
Yield descendent
Next
Yield child
Next
End If
End Function
End Module
-
Jul 6th, 2021, 06:55 AM
#46
New Member
Re: Visit Every Control on a Form (includes nested controls, no recursion)
Originally Posted by jmcilhinney
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.
-
Dec 20th, 2022, 06:20 AM
#47
Addicted Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|