proxyuser said:
I didn't know that setting the value was supposed to do that. However, it
doesn't really explain what I'm seeing. The doc also says "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." So then why is
the FormClosing event firing?
For better or worse, the event is overloaded. That is, you will get a
FormClosing and FormClosed event, even if the form is simply being
hidden, _if_ the form had been displayed modally.
For example, see the documentation for the FormClosing event: "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 FormClosing event of the form".
(
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx)
Since the documentation also says that a form that's _really_ been
closed (i.e. disposed) may not have a valid way to access the
DialogResult property (due to the form being disposed), the logical
conclusion is that you should expect the FormClosing event even if the
form isn't being disposed, i.e. when it's "closed" (i.e. hidden) in the
case of a modal dialog.
I think the API would have been more clear if there had simply been two
different classes, one for modal and one for modeless. But since they
are using the same class for either purpose, and since they didn't put
in events that are specifically for use with one purpose or the other,
the events have to do double-duty.
For a modal dialog, you will get the FormClosing and FormClosed events
when the window is being hidden, rather than when it's being disposed.
And of course, setting the DialogResult hides the window (as if the user
had dismissed it).
Note that the semantics are preserved across the entire API for the Form
class. The Close() method also only hides the window if the form was
being displayed modally. Only calling Dispose() will truly dispose the
form, if it had been shown modally. In that respect, the FormClosing
and FormClosed events are literally correct, and give the developer what
they really want in 99.9% of the cases.
Pete