views:

309

answers:

5

.NET 2

// dynamic textbox adding
myTextBox = new TextBox();
this.Controls.Add(myTextBox);

// ... some code, finally

// dynamic textbox removing
myTextBox.Dispose();

// this.Controls.Remove(myTextBox); ?? is this needed

Little explanation

  • Surely, if I Dispose a control I will not see it anymore, but anyway, will remain a "Nothing" in the parent controls collection?
  • need I also, like MSDN recommends, remove all handlers from the control?
+9  A: 

No, you don't.
I tried it.

You can paste the following code into LINQPad:

var form = new Form();
var b = new Button();
form.Controls.Add(b);
b.Click += delegate { b.Dispose(); };
Application.Run(form);

EDIT: The control will be removed from the form's Controls collection. To demonstrate this, replace the click handler with the following:

b.Click += delegate { b.Dispose(); MessageBox.Show(form.Controls.Count.ToString());};

It will show 0.

2nd EDIT: Control.Dispose(bool disposing) contains the following code:

                if (parent != null) { 
                    parent.Controls.Remove(this); 
                }
SLaks
Why was this downvoted?
SLaks
+1 Good work. Its interesting that the msdn articale kbrinley posted mentions calling Remove then Dispose. Looks like you only need Dispose. Good to know.
SwDevMan81
yah. now, believe to MSDN, that recommends first to remove, and only then Dispose the control...
serhio
Calling `Remove` first will make your code easier to understand. However, it won't make any difference in the actual behavior.
SLaks
Thats what good old fashion comments are for :P
SwDevMan81
+2  A: 

EDIT:

MSDN suggests that you remove the object from the Control and then call dispose when removing an object from a collection at runtime:

http://msdn.microsoft.com/en-us/library/82785s1h%28VS.80%29.aspx

// dynamic textbox adding
myTextBox = new TextBox();
this.Controls.Add(myTextBox);

// ... some code, finally

// dynamic textbox removing
this.Controls.Remove(myTextBox);

myTextBox.Dispose(); 
kbrinley
This is completely false. You should _always_ call `Dispose` manually to ensure that native resources (in the case, a window handle) are released as soon as they are no longer needed. Relying on the finalizer to call `Dispose` will cause the native resources to be released much later, when the GC runs.
SLaks
@kbrinley: Sometimes, MSDN tells general thinks that is not necessarily optimized etc etc...
serhio
I've found an MSDN article regarding this exact situation, which does indeed suggest calling Remove then Dispose.
kbrinley
MDSN tells also to remove handlers from the control... so...
serhio
@SLaks: I thought the resources were kept in memory until it was full, then the manager performs a cleanup. If that is the case, how would calling dispose be any benefit other than prematurely calling GC features?
diadem
@diadem - You are right, the resources will be kept in memory until the GC is called, howerer it will not call Dispose in the normal manor. This (http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae) is a great sight for understanding the differences. Basically the GC will call the Finalizer functions of each unreferenced object when it decides to clean up.
SwDevMan81
A: 

Just keep in mind that if you have some code to iterate over your controls and do something, you would get an exception if one of these controls had been disposed. Therefore, in general I would probably recommend removing the control as good practice.

statichippo
No, you wouldn't. `Dispose` will remove the control from the `Controls` collection. I checked.
SLaks
@hippo: apparently, the disposed controls are automatically removed from the parent control collection.
serhio
Ok, you learn something new everyday! :)
statichippo
that's what i meant
statichippo
+1  A: 

After some tests, I find out that the disposed controls are automatically removed from the parent control collection.

Controls.add(myButton); //Control.Count==4
myButton.Dispose(); //Control.Count==3

UPDATE

from the control's Dispose(bool) method:

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

Further Information on Compact Framework 2 + VS2005 Designer may crash when removing a control which is derived from s.w.f.control, if it doesn't implement the following:

Dispose()  
{  
 if(this.parent!=null){  
  this.parent.controls.remove(this);   
 }  
 ....  
}  
Mat