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?

Fernando
  • 1,239
  • 1
  • 16
  • 27

2 Answers2

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;
        }
    }
John Lockwood
  • 3,787
  • 29
  • 27
  • Note: In WPF you can not change `Window.DialogResult` after form is closed. – Zéiksz Dec 18 '14 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. :) – John Lockwood Dec 18 '14 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 '14 at 08: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 '18 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

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
  • 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 '11 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. – Evan Mulawski Aug 23 '11 at 17:15