Couldn’t the following code be done (and done better) with LINQ/func delegate? I’m fairly new to LINQ/func. I’m looping through all the controls on the form to find all the FlowLayoutPanels (they’re embedded in tabs and split containers) and saving the control index information to the application settings so it can be recalled upon loading. My app allows the user to reposition their GUI/controls with drag drop. *Note, ServicingLayout is my own serializable class with info I need to restore the user’s preference.
Private Sub SaveFlow(ByVal F As FlowLayoutPanel) Handles Me.FoundFlow
For Each C As Control In F.Controls
My.Settings.ServicingLayout.AddControl(F.Name, C.Name, F.Controls.GetChildIndex(C))
Next
End Sub
Private Event FoundFlow(ByVal F As FlowLayoutPanel)
Private Sub SaveFlowLayouts(ByVal CC As Object)
For Each C As Control In CC
If TypeOf C Is FlowLayoutPanel Then RaiseEvent FoundFlow(C)
If C.Controls.Count > 0 Then SaveFlowLayouts(C.Controls)
Next
End Sub
Thank you!
Olivier is right in suggesting that LINQ is good for processing data through filters and applying transformations, etc… it doesn’t do other things (like recursion, multi-branch logic) so well.
I was kind of able to do this in LINQ, but it is ugly.
However, I would recommend stealing at least one LINQ feature… you can easily filter the
ControlCollectionforFlowLayoutPanelwith this line (and as a bonus it’s already strongly typed for you!)Without further-ado… ugly LINQ.
Part of the ugliness is due to the fact that Control.ControlCollection is not strongly typed (it implements
IEnumerableinstead ofIEnumerable(Of T), so we have to tell LINQ the type of the objects in that collection (more detail here).