I have two Forms: Form1 and Form2.
Form1 is a main Form. I create a button on Form1 to show Form2.
If I show Form2 like a modeless form (using method Show()), its Dispose method is called immediately after closing it.
But if I show Form2 like a modal form (using method ShowDialog()), its Dispose method is not called after closing it. It is only called after closing Form1 (main form) and exit application.
//code Dispose method of Form2
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
Note: I use Debug to trap in Dispose method.
How to call Dispose() immediately when closing a modal form.
Please explain it. Thanks.
You have to manually call
.Dispose()in your code in Form1 right after the.ShowDialog().