oForm.Close() vs this.Close() in a modal dialog.

D

dan.c.roth

oForm.Close() vs this.Close() in a modal dialog.

oFrom.Close() calls Form.Dispose() but this.Close() ,say in the click
event of the Form, does not.

I can think of the reason for this but is this documented and if so can
someone give me the reference. And is this true for .net 1 and 2.

Thanks

Dan
 
G

Guest

Hi Dan,
when a form is closed then Dispose is called as you mentioned so that all
of the resources associated with the form are cleaned up, from the msdn
documentation:

"When a form is closed, all resources created within the object are closed
and the form is disposed."

When you are in a modal dialog typically after the form is closed you want
to retrieve values from the form, so you do not want the form to be Disposed,
which is why the dispose is not called when called internally. You need to
remember to call Dispose after closig the modal form, or put it inside a
using statment.

This is the same for both 1.1 and 2.0. As for a specific document stating
the above try: http://msdn2.microsoft.com/en-us/library/c7ykbedk.aspx

which states:

"When a form is displayed as a modal dialog box, clicking the Close button
(the button with an X at the upper-right corner of the form) causes the form
to be hidden and the DialogResult property to be set to DialogResult.Cancel.
Unlike modeless forms, the Close method is not called by the .NET Framework
when the user clicks the close form 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 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."


Mark.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top