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(..开发者_开发知识库.);
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);
精彩评论