Both the panel and the form class have the Controls collection property, which has the Clear () method ...
MyPanel.Controls.Clear();
or
MyForm.Controls.Clear();
But Clear() does not call dispose() (all it does is remove it from the collection), so you need to do
List<Control> ctrls = new List<Control>(MyPanel.Controls); MyPanel.Controls.Clear(); foreach(Control c in ctrls ) c.Dispose();
You need to create a separate list of links because Dispose will also remove the control from the collection by changing the index and ruining the foreach ...
Charles Bretana
source share