Tuesday, August 4, 2015

Remove controls from FlowLayoutPanel



How to remove all the controls on the FlowLayoutPanel Control


Today I will show to you the best way to remove any controls inside a FlowLayoutPanel 

How to create a project in Visual Studio?

Exists many ways to create a new project in VS. For example, when you open your VS program, in the Start Page you will see the option New Project ... 
If you can´t see the Start Page you can also click in File > New Project > Select Visual Basic > Windows Forms Application
Rename your project name if you want and click OK.
It´s easy! 

Code

For this demonstration, in the Designer View of the Form add some controls like Buttons, RadioButton ... anything, inside of the FlowLayoutPanel.
Now, add a button out of the FlowLayoutPanel. Set the text of this button to "Clean"
Double-click the button (to open the code file and create the event .Click )

Paste the next code

WhileFlowLayoutPanel1.Controls.Count > 0 'Determine if the flow contains any controls
            Dim controltoremove = FlowLayoutPanel1.Controls(0)
            FlowLayoutPanel1.Controls.Remove(controltoremove)
            controltoremove.Dispose()
End While

Now press F5 to Debug the application. 
Click the button and you will see all the controls disappear.

No comments:

Post a Comment