Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / October 2004

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Amil - 13 Oct 2004 16:15 GMT
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?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.