开发者

WinForms Close() sets DialogResult to Cancel

If I call Close() in my WinForm, it seems that even though Dial开发者_C百科ogResult is None at the time, right after I call Close() I see that it is set to Cancel.

Does this sound normal?


Or even easier, you can set DialogResult just after Close. For example, assuming ValidateSettings will show the user any problems with the form or return true otherwise:

    private void btnOK_Click(object sender, EventArgs e)
    {
        if (ValidateSettings())
        {
            SaveSettings();
            Close();
            DialogResult = DialogResult.OK;
        }
    }


That is completely normal, as it is the intended behavior. However, it is not equivalent to clicking the red "X" in the top right corner of the Form if you are using an MDI or ShowDialog().

When a form is displayed as a modal dialog box, clicking the Close button (the button with an X in the top-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. The Close method is not automatically called when the user clicks the Close button of a dialog box or sets the value of the DialogResult property. Instead, the form is hidden and can be shown again without creating a new instance of the dialog box. Because of this behavior, you must call the Dispose method of the form when the form is no longer needed by your application.

The DialogResult value can be overridden though:

You can override the value assigned to the DialogResult property when the user clicks the Close button by setting the DialogResult property in an event handler for the Closing event of the form.

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult(v=VS.100).aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜