Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a form Disposed when the user closes it from window bar/etc

Tags:

c#

.net

winforms

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(...);

like image 598
Anonym Avatar asked Jan 11 '10 12:01

Anonym


People also ask

Does form close call Dispose?

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.

What is form dispose?

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.

How do I close a Windows 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.


1 Answers

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);
like image 73
Marc Gravell Avatar answered Sep 23 '22 21:09

Marc Gravell