6

I'm building a flow layout panel whose each control represents for a room. I want to reload all room by removing all controls in the panel and adding new controls.

I used:

foreach(Control control in flowLayoutPanel.Controls) 
{
    flowLayoutPanel.Controls.Remove(control);
    control.Dispose(); 
}

but some of controls couldn't be removed.

I tried to find a solution on the internet but found nowhere.

Could any body help?

cuongle
  • 74,024
  • 28
  • 151
  • 206
Johnny
  • 257
  • 2
  • 3
  • 6
  • Was there an error, such as a thrown exception or message in the Output window? If so, please add that to the question? – akton Oct 01 '12 at 03:53
  • 1
    There was no exception or message in the Output window, Akton. Just one thing, some of controls were still displayed in the panel. – Johnny Oct 01 '12 at 04:13
  • possible duplicate of [Properly disposing of, and removing references to UserControls, to avoid memory leak](http://stackoverflow.com/questions/12610535/properly-disposing-of-and-removing-references-to-usercontrols-to-avoid-memory) – Hans Passant Oct 01 '12 at 05:50

4 Answers4

13

According to MSDN, you can clear all controls from a ControlCollection (such as a FlowLayoutPanel) by calling the Clear() method. For example:

flowLayoutPanel1.Controls.Clear();

Be aware: just because the items are removed from the collections does not mean the handlers are gone and must be disposed of properly less you face memory leaks.

ShooShoSha
  • 956
  • 10
  • 17
  • 1
    By far the simplest and fastest. I didn't use this at first because I needed to retain some panels while clearing the rest. But the other methods were too slow thus I added the panels I needed to keep to a list and re-added them after the clear, thanks. – Anthony Griggs Oct 14 '19 at 15:51
  • Yeah. this method is a fast and a clean way to remove all the controls in a flowlayoutpanel. Please mark this as the answer. – LordDraagon Nov 30 '19 at 07:08
12

That's because you are removing the controls from the same list you are iterating. Try something like this

List<Control> listControls = flowLayoutPanel.Controls.ToList();

foreach (Control control in listControls)
{
    flowLayoutPanel.Controls.Remove(control);
    control.Dispose();
}

Maybe not like that, but you get the idea. Get them in a list, then remove them.

oberfreak
  • 1,799
  • 13
  • 20
Yoinazek
  • 133
  • 5
  • 4
    You need to have System.Linq on your usings. If you still don't have it after that, you need to cast the object. In this case 'flowLayoutPanel.Controls.Cast().ToList()' – Yoinazek Oct 25 '12 at 16:41
6

Note: this is a working solution based on the previous comment, so credits to that person :)

This worked for me:

List<Control> listControls = new List<Control>();

foreach (Control control in flowLayoutPanel1.Controls)
{
     listControls.Add(control);
}

foreach (Control control in listControls)
{
     flowLayoutPanel1.Controls.Remove(control);
     control.Dispose();
}

There is probably a better/cleaner way of doing it, but it works.

Fakekiller17
  • 61
  • 1
  • 2
0

If you are looking for an easy and quick solution. Here it is.

while (flowLayoutPanel.Controls.Count > 0) flowLayoutPanel.Controls.RemoveAt(0);
Lasitha Yapa
  • 4,309
  • 8
  • 38
  • 57