Anonym
Anonym

Reputation: 3118

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

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

Upvotes: 10

Views: 332

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062770

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

Upvotes: 14

Related Questions