29

If I call Close() in my WinForm, it seems that even though DialogResult is None at the time, right after I call Close() I see that it is set to Cancel.

Does this sound normal?

0

2 Answers 2

43

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;
        }
    }
4
  • Note: In WPF you can not change Window.DialogResult after form is closed.
    – Zéiksz
    Dec 18, 2014 at 10:30
  • 3
    @Zéiksz - OK, but this is a Winforms question and the code was a Winforms answer that I tested in that environment. One might also down-vote it because it doesn't work on Ruby on Rails, but that seems a bit unfair. Or am I missing something because I've been working in Java so long. :) Dec 18, 2014 at 18:55
  • I did not downvote or something... just noted some WPF info. Bit offtopic, yes, but useful extension for your answer for those who are actually researching in this question (like I did, and thought others may use the info too - right here). I'm sorry if you took it as offense, say so and I remove the comment.
    – Zéiksz
    Dec 19, 2014 at 8:53
  • This should be marked as the right answer as most people are closing a single dialog under normal circumstances and don't generally want to override the default behavior.
    – Wilmer SH
    Feb 14, 2018 at 16:58
26

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

2
  • 1
    Is there any documentation that actually describes the normal, intended behavior of the Close() method setting the DialogResult value to Cancel?
    – Fernando
    Aug 23, 2011 at 16:38
  • Not that I could find. However, the only difference between Close() and clicking the close button, is that the Form is not disposed if the conditions above are met. That means the assignment to DialogResult is the same (Cancel). If you need it to be None, it can be overridden using the method above. Aug 23, 2011 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.