Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove TabPage: Dispose or Clear or both?

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 (int i = tabDocuments.TabPages.Count - 1; i > -1; i--) {
    tabDocuments.TabPages[i].Dispose();
}
    tabDocuments.TabPages.Clear();

The person who wrote this code has already left a while ago. I am trying to understand why the code is calling Clear() after disposing each of the tabPages (looks un-necessary to me). Can anyone please explain to me why? Or is calling Clear() extra?

like image 377
David Avatar asked Nov 18 '09 16:11

David


1 Answers

This snippet is from Control.Dispose:

        if (this.parent != null)
        {
            this.parent.Controls.Remove(this);
        }

Therefore you just have to call Dispose, not Clear.

like image 138
Philip Wallace Avatar answered Oct 15 '22 02:10

Philip Wallace