![]() |
Click here to advertise with us
|
|
|||||||
| Windows Forms Discussion related to Winforms application development |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Is it possible to cancel the "cancel-event" when pressing ESC in a dialog box?
Here's the situation: I have a form where I have a cancel and Ok button (and of course lots of other non-important-for-this-case controls). This dialog box should be like any other dialog box, if you press ESC then the form closes and dialogresult should be Cancel. This part is easy, set the cancel button to have dialogresult cancel and the forms CancelButton property should point to this button The problem is that I want the ESC key to do two different things, depending on situation. Under normal situations it should just close the form, but when I have a certain thread running on the form, I want the ESC key stop this thread (but still leave the form open) The problem is that unfortunately MS has made it so that when the CancelButton is set on the form, the form closes after the code in the Cancel button has been fired. I haven't found any ways to stop the form from closing after ESC key has been pressed, being able to this would be the best solution IMHO One solution to do what I want to do is this: Remove the above mentioned CancelButton from the property on the form and handle the KeyDown Event to check for the ESC key, this gives me complete control. The negative side is that I pretty much need to handle the KeyDown Event on all controls on the form Is my above mentioned solution the best way to do this? Or is there are way to stop the form from closing after ESC key has been pressed? Or is there some other much better solution I haven't thought about? |
|
|||
|
Quote:
Thanks for the tip ![]() Quote:
I just found a could be solution for that problem. Setting the KeyPreview on the form to true, the forms KeyUp (or KeyDown) event is fired when ESC is pressed, no matter which control is activated... that should take care of that problem |
|
||||
|
You can use the FormClosing event and e.Cancel it depending on e.CloseReason and your own thread work reason.
Note that the application user is in command, so if user want to close it regardless of what goes on she should be allowed to do so without needing to kill the process. What you can do is to check if work is in progress and if user tries to close you present a dialog that tell user what is going on and give option to continue/close, if user chooses to close anyway you should stop that thread work right away and allow the dialog form to close. You can also do this without "another dialog from the dialog" by making it clear what is happening with for example a statusstrip with status text and progress, which makes it obvious to user if cancelled before 100% whatever is going on will not complete.
__________________
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly ![]() |
|
|||
|
In my example closing the form should without warning kill the thread (like if the user presses the cancel button or the big fat red X). It's just that from the users point of view the thread (which searches for items) should end if the ESC key is pressed (or close the form if no search is in progress)
Seems like the above solution works great |
|
||||
|
Note: please don't kill the thread.. Do your searching with a BackgroundWorker with SupportsCancellation set to TRUE, and often during your search, check the _backgroundWorker.CancellationPendign property. If it is True, Return from DoWork.
This is the proper way to stop background operations. Killing threads is like using task manager End Process to quit your apps; it can leave things in a proper mess |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|