Remove TabPage: Dispose or Clear or both?

前端 未结 3 1281
栀梦
栀梦 2021-01-19 16:33

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         


        
相关标签:
3条回答
  • 2021-01-19 17:08

    This snippet is from Control.Dispose:

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

    Therefore you just have to call Dispose, not Clear.

    0 讨论(0)
  • 2021-01-19 17:08

    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().

    0 讨论(0)
  • 2021-01-19 17:12

    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.

    0 讨论(0)
提交回复
热议问题