Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Dispose() to remove Control from Form

Is it correct to use Dispose() to remove a Control from a Form?

E.g. I have a button 'button1' on my formular. When calling Dispose() it immediately disappears from the form and also from the 'Controls'-Collection of the form. But is this always the case? Or are there other cases (maybe other controls) where the GC waits some time?

like image 669
user1027167 Avatar asked May 14 '26 03:05

user1027167


1 Answers

This "bonus" of Control.Dispose is not documented.

As a general tip, you should not build your program around expectations that undocumented behavior stays the same for the future, or even across all current control implementations.

However, as you can see from the Reference Source of Control.Dispose(bool):

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

This does indeed happen in the current implementation.

But again, this is not documented behavior. Make of it what you will.

like image 133
Lasse V. Karlsen Avatar answered May 16 '26 18:05

Lasse V. Karlsen