MWild
MWild

Reputation: 1

Dynamically clearing a flow layout panel

been trying to do this a couple different ways, this is my latest:

Dim i As Integer = 0
    Dim ControlCount As Integer = SearchResults.Controls.Count
    Do Until i > ControlCount
        SearchResults.Controls.RemoveByKey(i)
        i += 1
    Loop

Search Results being the name of the flow layout panel!

any ideas of how to do this? i simply want to remove everything that is currently in the panel!

ive also tried things like searchresults.items.clear aswell but to no avail

Upvotes: 0

Views: 2962

Answers (2)

The key is a string as the first response stated. The key is the Name property of the control. If you also want to remove the flowlayoutpanel as well as it's contents you can call the dispose method like so:

    If IsNothing(flpTechProductionNumbers.Controls.Find(Action, True).FirstOrDefault) Then
    Else
        flpTechProductionNumbers.Controls.Find(Action, True).First.Dispose()
        ' flpTechProductionNumbers.Controls.Find(Action, True).First.Controls.Clear()
    End If

If you want to access the controls via an index like you are trying, you should be able to treat the Controls property as an array and do something like this:

            flpTechProductionNumbers.Controls(i)

Upvotes: 0

Andrew Morton
Andrew Morton

Reputation: 25066

Have you tried SearchResults.Controls.Clear()? A FlowLayoutPanel has a Controls property. Looking in the documentation for that ( http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls.aspx ), you can see that is a Control.ControlCollection, and looking in the docs for that, you can find the .Clear() method.

I suspect your controls do not have keys of 0,1,2... (the key should be a String, not an Integer anyway) - perhaps you were thinking of the RemoveAt method ( http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.removeat.aspx ). But that wouldn't work in the way you've shown because each time you remove a control the number of controls is reduced by one.

So, try the .Clear() method, and please set Option Strict On, preferably in the IDE, to let it point out errors for you.

Upvotes: 2

Related Questions