Is a System.Windows.Forms.Form automatically disposed when the user closes it with the top right X, or Alt+F4 ? The form is shown with form.Show(this), not form.ShowDialog(...);
After you close the form by Close or by clicking on X, it will be disposed automatically. Did you open the form using ShowDialog ? If you want to reuse it later, then do not forget to explicitly Dispose in when you no more need it.
Dispose() destroys the dialog and frees its resources back to the operating system. It does not call the form's Closing() and Closed() methods. Once disposed, you may not recall a form.
The Form. Close() function is used to close a Form in a Windows Form application in C#. We can use the Form. Close() function inside the button click event to close the specified form by clicking a button.
With Show
, yes it is (at the end of WmClose
). With ShowDialog
, no it isn't. Fun ;-p
For ShowDialog, see MSDN:
Because a form displayed as a dialog box is not closed, you must call the Dispose method of the form when the form is no longer needed by your application.
To prove it, though:
Form main = new Form();
Form test = new Form();
test.Text = "Close me";
test.Disposed += delegate {
main.Text = "Second form was disposed";
};
main.Shown += delegate {
test.Show();
};
Application.Run(main);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With