How to remove controls from a container without updating the container

I have a regular Panel control with a set of user controls contained inside. At the moment, I am doing the following:

panel.Controls.Clear();

but this leads to the fact that I see (albeit quickly) that each control disappears individually.

Use SuspendLayoutand ResumeLayouthas no noticeable effect.

Question: Is there a way to remove ALL controls and update the container only when all child controls have been deleted?

Edit: The controls that I remove are derived from UserControl, so I have some control over their drawing. Is there some kind of function that I could override to prevent the update from uninstalling?

+5
source share
2 answers

Thank you, Hans for your proposal - yes, it turns out I missed the control.

Here is 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 has no borders) before I remove each control. Recycling each control automatically removes the specified control from the parent container, which is nice. Finally, I make the container visible again.

+5
source

I think you need double buffering.

There are several answers regarding this already on SO, sort of

Winforms Double Buffering , Enabling Double Buffering

and

# ( Windows)?

SuspendLayout , , , ResumeLayout. , . , . , , , , .

0

All Articles