user1027167
user1027167

Reputation: 4458

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?

Upvotes: 3

Views: 2339

Answers (3)

Harald Coppoolse
Harald Coppoolse

Reputation: 30502

Should you call Dispose()?

Whenever you see any class that implements System.IDisposable you should assume that the designer of the class thinks that the class might hold some scarce resources.

You are not obliged to call Dispose() of objects of these classes. If you don't, the finalizer will. However if you don't call Dispose(), the scarce resource is held longer than needed, possibly leading to insufficient resources.

So indeed, before assigning null to your Control you should call Control.Dispose().

Should you remove the control from the control collection before disposing the control?

Apparently the current implementation makes sure that disposed controls are removed from the control collection in control.Parent.Controls.

However, depending on your implementation, are you sure that the ControlCollection where you added your control is not a subclass of System.Windows.Forms.Control.ControlCollection, with a slightly different behaviour? After all: ControlCollection is not sealed. Even if you made your own form with your own ControlCollection, people might derive from your form with its own ControlCollection.

Hence, to make sure that yo follow the rules of handling the ControlCollection I think you should remove the control from the collection before disposing it.

// Create myControl and add to myForm
var myControl = new MyControl(...);
myControl.SetProperties
myForm.Controls.Add(myControl);

// proper removal:
// if someone else could already have removed the control:
// add a check if it is still there.
myForm.Control.Remove(myControl);
myControl.Dispose();    // no problem if already disposed
myControl = null;

Upvotes: 1

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391634

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.

Upvotes: 3

user1968030
user1968030

Reputation:

From MSDN:

This method is called by the public Dispose method and the Finalize method. Dispose invokes the protected Dispose(Boolean) method with the disposing parameter set to true. Finalize invokes Dispose with disposing set to false.

So I think no problem.

Upvotes: 3

Related Questions