Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove(), Clear() or Dispose()

Tags:

.net

winforms

Simple question...

I have controls that the user can drag around on my form at runtime. And they can also delete them... Should I just Call .Dispose(); when they click the delete button, or should I do something like panel1.Controls.Clear(Control); ? ...Or something else?

Thanks :)

Bael

like image 733
jay_t55 Avatar asked Feb 06 '10 15:02

jay_t55


2 Answers

You should remove it from the parent Controls collection as described in Darin Dimitrov's response, and also call Dispose:

panel.Controls.Remove(someControlInstance);
someControlInstance.Dispose();

You should always call Dispose on objects that implement IDisposable when you have finished with them, so that any unmanaged resources they own are immediately released.

like image 161
Joe Avatar answered Nov 15 '22 07:11

Joe


Just remove the control from the panel:

panel.Controls.Remove(someControlInstance);

Once there are no more references to it, it will be subject to garbage collection and unmanaged resources will be properly disposed.

like image 20
Darin Dimitrov Avatar answered Nov 15 '22 06:11

Darin Dimitrov