Hi all,
I have a form that has two buttons: btnOK and btnCancel. This form does not
show the Controlbox and you can only close it by clicking on either buttons.
The code in both buttons to close the form has a single line that says:
Dispose(); Is this the proper way to close or release the form?
Imran Koradia - 13 Oct 2004 16:39 GMT
Dispose() is not the right way to close down a form. Call the Close() Method
on the form if you're done with. If you just want to be able to hide it, you
can use the Hide() method - this will keep the form in memory in its
current state which you can then show again using the Show() method.
hope that helps..
Imran.
> Hi all,
>
> I have a form that has two buttons: btnOK and btnCancel. This form does not
> show the Controlbox and you can only close it by clicking on either buttons.
> The code in both buttons to close the form has a single line that says:
> Dispose(); Is this the proper way to close or release the form?
Herfried K. Wagner [MVP] - 13 Oct 2004 17:15 GMT
"Amil" <Amil@discussions.microsoft.com> schrieb:
> I have a form that has two buttons: btnOK and btnCancel.
> This form does not show the Controlbox and you can only
> close it by clicking on either buttons. The code in both
> buttons to close the form has a single line that says:
> Dispose(); Is this the proper way to close or release the form?
Call the form's 'Close' method ('Me.Close()'). Notice that forms that are
shown using 'ShowDialog' are disposed automatically when being closed, on
the other hand, forms shown using 'Show' are not disposed automatically.

Signature
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/
Chris Dunaway - 15 Oct 2004 15:10 GMT
> Notice that forms that are
> shown using 'ShowDialog' are disposed automatically when being closed, on
> the other hand, forms shown using 'Show' are not disposed automatically.
I think you got that backwards, Herfried! Forms shown with ShowDialog are
*not* disposed automatically. That's so you can get the DialogResult from
them. Forms shown with Show are disposed automatically when closed.

Signature
Chris
dunawayc[AT]sbcglobal_lunchmeat_[DOT]net
To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Herfried K. Wagner [MVP] - 15 Oct 2004 15:31 GMT
"Chris Dunaway" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> schrieb:
>> shown using 'ShowDialog' are disposed automatically
>> when being closed, on the other hand, forms shown using 'Show'
[quoted text clipped - 4 lines]
> get the DialogResult from them. Forms shown with Show are
> disposed automatically when closed.
Ooops... Thanks for the correction, it seems that my fingers type things
that don't correspond with what I am thinking ;-).

Signature
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/
Stoitcho Goutsev \(100\) [C# MVP] - 16 Oct 2004 16:49 GMT
Hi Amil,
Other posters gave you the correct answer of your question already however,
I'd like to throw one other idea, which is applicable only to modal forms
(ShowDialog)
When you close a modal for the correct way to close the form is to set
form's DialogResult property to some of the DialogResult enum's values
different than DialogResult.None.
This will close the form, but not dispose it as long as with modal forms is
normal that you want to get some of its control's values back to the caller.
In other words the form is rather hidden. The DialogResult value you set is
going to be returned as a ShowDialog method's return value.
The caller must dispose the form when it's done. If you don't do any
processing on the button's (Ok, Cancel, Yes, No or whatever buttons you
have) Click event and the only thing you do is to close the dialog instead
of hooking on their Click event you can set buttons' DialogResult property
to relevant DialogResult value. This way when you click on the button the
framework will close the form and set form's DialogResult property for you.
You may alse take a look on the form's AcceptButton and CancelButton
properties. The former is the button, which click will be simulated when the
user presses ESC key and the latter is the button, which click will be
simulated when the user presses Enter key.

Signature
HTH
Stoitcho Goutsev (100) [C# MVP]
> Hi all,
>
[quoted text clipped - 4 lines]
> The code in both buttons to close the form has a single line that says:
> Dispose(); Is this the proper way to close or release the form?