Quote:
you NEED the "break" line so it doesn't continue to loop and start referencing null values.
More importantly, it'll throw an exception when you try to iterate over a modified collection.
The other thing you need to know is that, if 'this' is a System.Windows.Forms.Panel, there's a bug in .NET 1.1 (might be fixed in 2.0) which -- if you're doing repeated Controls.Add and Controls.Remove actions -- causes your app to not exit properly (you can click on the Close button until the cows come home, and nothing happens).
To fix it, you need something like this:
Code:
void RemoveControlFromPanel(Panel p, Control c)
{
p.Controls.Remove(c);
if (!c.IsDisposed)
c.Dispose();
}
void RemoveAllControlsFromPanel(Panel p)
{
foreach (Control c in p.Controls)
{
if (!c.IsDisposed)
c.Dispose();
}
p.Controls.Clear();
}