I am working on a windows form that has a TabControl named tabDocuments. I came across this piece of code that removes all pages from the TabControl.
for (in
This snippet is from Control.Dispose:
if (this.parent != null)
{
this.parent.Controls.Remove(this);
}
Therefore you just have to call Dispose, not Clear.
First remove a Tab from the collection, then Dispose(). Never Dispose() something that is still in use, as it will cause exceptions and strange behavior.
Also, ensure that no one else have references to the tabs, otherwise those references will become invalid on Dispose().
Calling Dispose() on each of the tab pages does not actually remove them from the TabPages collection, it simply disposes them. The call to Clear() is what removes them from the collection. If you don't call Clear() they will still be there, and bad things will likely happen because you will end up trying to use them after they have been disposed.