What is the proper way to close a form without a controlbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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.
 
Amil said:
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.
 
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.

--
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.
 
Chris Dunaway said:
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.

Ooops... Thanks for the correction, it seems that my fingers type things
that don't correspond with what I am thinking ;-).
 
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.
 
Back
Top