What's the name of the OnDispose control - C #
My designer.cs file has this stub:
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
I have a C # application that has a form in which I call my custom control. When I close the form, do I need to explicitly call the usercontrol.Dipose method?
When you close a form, it may not have its own method Dispose
called immediately by the garbage collector (if it doesn't live in a block using
).
However, it suffices to call the Dispose
form's method , since this will (eventually) call Dispose
for all controls in its collection Controls
(and each control in turn will call Dispose
on all controls in their collection of Controls, etc.), so it ends up also your Dispose method is called UserControl
.
source to share
If the object is not in the use block, then yes, you should call the dispose method.
That being said, you can include the dispose method in the finalizer of the control object, so that when the object is disposed by the GC, the dispose method is called. This type defeats the purpose of using the Dispose method (since the dispose method is used to dispose of child objects before the parent goes to the GC), but you know the objects explicitly closed by this method.
bool disposed
~Object
{
if(!disposed)
Dispose();
}
source to share
When you close the form, if it was not open, then the framework will call its Dispose method. If it was opened by calling ShowModel, you must call Dispose using the seperatly method.
If your UserControl has been added to the collection of form controls, then when the Dispose method is invoked, it will also call Dispose on your custom control.
source to share