+6  A: 

Dispose() only cleans up unmanaged resources (although Paul Williams noted in the comments that it is usually more complex than this!) so it may or may not do anything useful in your case.

Try removing the controls with the RemoveAt(i) method, not Dispose():

for(int i = panel.Controls.Count-1; i >= 0; i--)
{
    panel.Controls.RemoveAt(i);
}
Dan Herbert
Or you can use panel.Controls.Clear() if you want to remove them all. Control.ControlCollection will take care of clean up if necessary.
Patrik
thank you patrik
baeltazor
thank you Dan Herbert
baeltazor
A proper overridden Dispose(boolean) method disposes of both managed and unmanaged resouces. Dispose(true) means dispose of both. Dispose(false) means you have a finalizer, and the finalizer called the Dispose method. If called from a finalizer, the Dispose method must not touch any other managed objects. It's too late for that.
Paul Williams
+8  A: 

A simpler way to delete all your controls is to do this:

panel.Controls.Clear();
MusiGenesis
thank you MusiGenesis :)
baeltazor
+1  A: 

I have seen this before, you are removing items from acollection that make the collection itself smaller. e.g if there are 5 items in the collection as you move down through it you come to the end of the list sooner than you expect because the list gets smaller with every Dispose() you issue.

Darrin Lynn
Not sure that's going to apply in a foreach loop.
Lazarus
thank you Darrin that was much appreciated. I understand what's going on now. Thanks again. :)
baeltazor