473,766 Members | 2,172 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

closing a form on escape

Hi,
I want to close my form when the user presses the Escape key.
I tried to catch the event KeyPress of the form but the program do not go
through this code.
I tried to catch the KeyPress event for several conrtrols in the dialog and
the event was caught, but I do not want to catch this event for every
control in the form.

What is the right way to close the form when the user presses Escape ?

Yoav.
Aug 18 '08 #1
4 16419
Set the Form's CancelButton to a Button instance that has the
DialogResult property set to DialogResult.Ca ncel; this will cause the
form to be closed with a cancellation result.

Marc
Aug 18 '08 #2
Yoavo wrote:
Hi,
I want to close my form when the user presses the Escape key.
I tried to catch the event KeyPress of the form but the program do not go
through this code.
I tried to catch the KeyPress event for several conrtrols in the dialog and
the event was caught, but I do not want to catch this event for every
control in the form.

What is the right way to close the form when the user presses Escape ?
If the form is a dialog, then in probably some sort of a Close or
Cancel button (it is a good idea to include at least Close, anyway,
per the UI guidelines). If so, you can set the form's CancelButton
property to that button, and it will be automatically pressed (and its
Click event handler called) when the user presses ESC. This is the
preferred way.

If you'd really rather have a form without Close, then you should set
its KeyPreview property to true - then, it will receive any key event
before it reaches the child controls.
Aug 18 '08 #3
On Aug 18, 7:05*am, "Yoavo" <y...@cimatron. co.ilwrote:
Hi,
I want to close my form when the user presses the Escape key.
I tried to catch the event KeyPress of the form but the program do not go
through this code.
I tried to catch the KeyPress event for several conrtrols in the dialog and
the event was caught, but I do not want to catch this event for every
control in the form.

What is the right way to close the form when the user presses Escape ?
Try using the KeyDown Form event. Works for me.

private void Form1_KeyDown(o bject sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
Application.Exi t();
}
Aug 18 '08 #4
On Aug 18, 7:05*am, "Yoavo" <y...@cimatron. co.ilwrote:
Hi,
I want to close my form when the user presses the Escape key.
I tried to catch the event KeyPress of the form but the program do not go
through this code.
I tried to catch the KeyPress event for several conrtrols in the dialog and
the event was caught, but I do not want to catch this event for every
control in the form.

What is the right way to close the form when the user presses Escape ?

Yoav.
Hi,

You can use Form.ProcessCmd Key to handle any control key, like ESC,
enter, etc.
This allow you to decide what to do with more control
Aug 18 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
10336
by: Christopher Brandsdal | last post by:
Hi! I get an error when I run my code Is there any other way to get te information from my form? Heres the error I get and the code beneath. Line 120 is market with ''''''''''''Line 120''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
2
5757
by: Jax | last post by:
Is there any way to do this that doesn't involve creating a form to be a messagebox I'm re-writing a program that designed in VB and the users want to keep that functionality within the program Why could you do this with VB6 but not C# Many thanks to those that hel jax
1
2204
by: **Developer** | last post by:
When I get a closing event in a MID Child form I don't know if the child form is closing or the main form is closing. Is there a way to tell? Thank
6
1391
by: Joe | last post by:
Hi, I have a form with Submit and cancel button. When Cancel button is clicked I want to close the browser window. Here is my sample form. To make things simple I have removed the submit button. When I click on the cancel button, the window doesn’t close. Can someone tell me what mistake I am making here? Thanks for your help, Joe
10
4029
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms, remove the controls on each of them, and then close the form, but this code should execute only...
5
15786
by: Oenone | last post by:
I have a VB.NET form which I'm displaying modally using the ShowDialog() method. Within the form is a Cancel button, and I've set this button into the Form's CancelButton property so that pressing Escape automatically clicks the button. Within the button's Click event code I display a confirmation messagebox to ensure the user really does want to cancel. However, regardless of what code I put in this event, the dialog form always...
2
3022
by: Tom | last post by:
How is the best way to avoid validation when closing a window? For instance, I have a Windows Forms window which has a validation event for a text box. However, if one enters invalid data in then and then attempts to close the window (either via my custom 'Close' box or by clicking the close 'X' in the upper right window corner), the validation event still triggers and it tells the user that they have invalid data. Which of course means...
12
3841
by: colt28 | last post by:
Ok so I found an ajax contact form script but i can't get the b****** to work. I made a bunch of alterations to it and it didn't work so i replaced everything with the original and it still didn't work...dunno why, but anyhow. The form appears after a delay on the page in a hidden DIV. The original form just had name, email and message, but i have to add a few questions to it so i did and i also added some code from another script i found to...
7
2919
by: Sin Jeong-hun | last post by:
I have a dialog form which pops up from the main window using the ShowDialog() method. this dialog has no or button, and it has quite a lot of controls on it. Now, I want to close this dialog form when the user presses the escape key, but that's only when no control on the form is responsible for the escape key. For example, it has a ComboBox control, and a user can press the escape key just to close the drop down list that is being...
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10009
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8835
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.