If you want to delete all controls, you can iterate over the control collection back, rather than create a copy (see below).
I have found that this provides a better solution, especially if you intend to re-fill it. GC forced collection helps control memory where there are a large number of controls.
FlowLayoutPanel.SuspendLayout();
if (FlowLayoutPanel.Controls.Count > 0) {
for (int i = (FlowLayoutPanel.Controls.Count - 1); i >= 0; i--) {
Control c = FlowLayoutPanel.Controls[i];
c.SomeEvent -= SomeEvent_Handler;
c.Dispose();
}
GC.Collect();
}
FlowLayoutPanel.ResumeLayout();
source
share