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 / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Handle the form closing when the user clicks on the "X" button (form caption bar)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
polocar - 10 Nov 2006 14:59 GMT
Hi,
suppose that you have a C# form with two buttons, that are the
classical "btnOk" and "btnCancel" (besides them, of course in
the form there can be many other controls).
When the user clicks on btnOk, the program makes some confirm
operations and closes the form.
When the user clicks on btnCancel, the program makes some cancel
operations and closes the form.
My problem is that, when the user clicks on the "X" button of the
form caption bar, the form simply closes, while I would like that the
program made the same cancel operations as in the btnCancel click case.
I have discovered that the Form class has the "OnClosing" event
handler (that is executed when the form closes), but I can't insert
the cancel statements in its code (it is executed when the user clicks
on btnOk and btnCancel too, so the program would execute two times the
cancel operations or first the confirm operations and then the cancel
ones).
Is there a form event handler for the "X" form button click?
Or is there a form property that allows to associate the "X" form
button click to a button inside the form (btnCancel, in this case)?
Otherwise, can you propose me a simple way to handle this situation?
Thank you very much
DeveloperX - 10 Nov 2006 15:19 GMT
In the eventargs for OnClosing you'll find a cancel property. If you
set that to true the form will not close. Create a member variable set
to true and set it to false when the user clicks either the cancel or
ok buttons.

That's not the tidiest route, but it will work.

> Hi,
> suppose that you have a C# form with two buttons, that are the
[quoted text clipped - 18 lines]
> Otherwise, can you propose me a simple way to handle this situation?
> Thank you very much
Dave Sexton - 10 Nov 2006 16:03 GMT
Hi,

In the 2.0 framework use the FormClosing event instead of Closing, which is
now obsolete because it isn't raised when Application.Exit is called.  Same
goes for the FormClosed and Closed events.

"Form.FormClosing Event"
http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx

Signature

Dave Sexton

> In the eventargs for OnClosing you'll find a cancel property. If you
> set that to true the form will not close. Create a member variable set
[quoted text clipped - 25 lines]
>> Otherwise, can you propose me a simple way to handle this situation?
>> Thank you very much

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.