Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove controls from a container without container updating

I have an ordinary Panel control with a bunch of user controls contained within. At the moment, I do the following:

panel.Controls.Clear();

but this has the effect that I see (albeit quickly) each control disappearing individually.

Using SuspendLayout and ResumeLayout does not have any noticeable effect.

Question: Is there a way I can remove ALL controls, and have the container update only when all child controls have been removed?

Edit: the controls I am removing are derived from UserControl, so I have some control over their drawing behaviour. Is there some function I could possibly override in order to prevent the updating as they are removed?

like image 835
Charlie Salts Avatar asked Aug 09 '11 01:08

Charlie Salts


1 Answers

Thank you Hans for your suggestion - yes, it turns out I was leaking controls.

Here's what I ended up doing:

 panel.Visible = false;

 while (panel.Controls.Count > 0)
 {
    panel.Controls[0].Dispose();
 }

 panel.Visible = true;

Basically, I hide the entire panel (which is border-less) before I dispose of each control. Disposing of each control automatically removes said control from the parent container, which is nice. Finally, I make the container visible once more.

like image 190
Charlie Salts Avatar answered Oct 05 '22 12:10

Charlie Salts