开发者

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜