473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 16398
Set the Form's CancelButton to a Button instance that has the
DialogResult property set to DialogResult.Cancel; 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(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
Application.Exit();
}
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.ProcessCmdKey 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
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...
2
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...
1
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
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....
10
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...
5
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...
2
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...
12
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...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.