Re: Visit Every Control on a Form (includes nested controls, no recursion)
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.
Re: Visit Every Control on a Form (includes nested controls, no recursion)
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.
Re: Visit Every Control on a Form (includes nested controls, no recursion)
Quote:
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
Re: Visit Every Control on a Form (includes nested controls, no recursion)
Quote:
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.
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