473,400 Members | 2,145 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,400 software developers and data experts.

C#:how to make Esc key to close form

40
hi
i need to close the form when esc key is pressed. i tried as below but no response.

Expand|Select|Wrap|Line Numbers
  1. private void Form1_KeyPress(object sender, KeyPressEventArgs e)
  2.         {
  3.             if(e.KeyChar==(char)27)
  4.             {
  5.                 this.Hide();
  6.             }
  7.         }
thanks in adv
kssk
Dec 27 '07 #1
6 47967
mzmishra
390 Expert 256MB
If you are closing the form try close() method
Dec 27 '07 #2
ssknov
40
If you are closing the form try close() method
ya
thats right.

but when i tried as

this.close(); //also it doesnt responds my Esc key press.

ssk
Dec 27 '07 #3
debasisdas
8,127 Expert 4TB
Try to handle that in the KeyDown event and also set the KeyPreview property of the form to True.
Dec 27 '07 #4
hi
i need to close the form when esc key is pressed. i tried as below but no response.

Expand|Select|Wrap|Line Numbers
  1. private void Form1_KeyPress(object sender, KeyPressEventArgs e)
  2.         {
  3.             if(e.KeyChar==(char)27)
  4.             {
  5.                 this.Hide();
  6.             }
  7.         }
thanks in adv
kssk

I tried this:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace test
  10. {
  11.     public partial class Form2 : Form
  12.     {
  13.         public Form2()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.  
  18.  
  19.         private void Form2_KeyDown(object sender, KeyEventArgs e)
  20.         {             
  21.             if (e.KeyValue==27)
  22.             {
  23.                 this.Hide();
  24.             }            
  25.         }
  26.  
  27.         private void Form2_Load(object sender, EventArgs e)
  28.         {
  29.  
  30.             Form2.ActiveForm.KeyPreview = true;           
  31.  
  32.         }
  33.  
  34.     }
  35. }
Also set the

Expand|Select|Wrap|Line Numbers
  1. this.KeyDown += new System.Windows.Forms.KeyEventHandler(Form2_KeyDown);
in the InitializeComponent() of the form
Dec 27 '07 #5
Just Try This and Let Me Know.

Vb Code
------------
Expand|Select|Wrap|Line Numbers
  1.     Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
  2.         Try
  3.             If msg.WParam.ToInt32 = Convert.ToInt32(Keys.Escape) Then
  4.                 Me.Close()
  5.             Else
  6.                 Return MyBase.ProcessCmdKey(msg, keyData)
  7.             End If
  8.  
  9.         Catch ex As Exception
  10.             Return False
  11.         End Try
  12.         Return MyBase.ProcessCmdKey(msg, keyData)
  13.     End Function
  14.  
C# Code
------------
Expand|Select|Wrap|Line Numbers
  1.         protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
  2.         {
  3.             try
  4.             {
  5.         if (msg.WParam.ToInt32() == (int)Keys.Escape)
  6.                 {
  7.             this.close();
  8.         }
  9.         else
  10.         {
  11.             return base.ProcessCmdKey(ref msg, keyData);
  12.         }
  13.             }
  14.             catch (Exception Ex )
  15.             {
  16.                 MessageBox.Show("Key Overrided Events Error:"+Ex.Message);
  17.             }
  18.             return base.ProcessCmdKey(ref msg,keyData);
  19.     }
  20.  
Apr 3 '09 #6
tlhintoq
3,525 Expert 2GB
Basically if the Form1 is not the control that has focus, then the keypress isn't being seen, so its not repsonding. maybe a textbox has focus.

Handling the Window Messaging as mkumar20xx has shown looks to be the better approach. While he gave you a solution, I thought it would be good to *understand* why it was a solution and why your approach wasn't working for you.
Apr 4 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: TinyTim | last post by:
I'm a newbie at ASP & HTML. It seems that when you use server side code and you're going to return a customized HTML form with several fields and labels, you have to do an extensive amount of...
4
by: Tonya | last post by:
Hi, Does anyone have any example of how i can manage forms in my application?? I want to be able to reference my form instances that are currently open from other forms. why cant i open...
2
by: Paul Brady | last post by:
I have non-computer skilled users entering data into a form. There are certain ranges of values which, if they enter them, make no sense in the application, but I can't test them until they try to...
5
by: RAJ | last post by:
hi plz tell me how to know "how window is going to close"... i have to right code for X button of forms... plz telll me thanks bye
7
by: Dave | last post by:
I have a button on Form1 that hides the form and displays Form2: Form2 myForm2 = new Form2(); myForm2.Show(); this.Hide(); After I do some work in Form2 I want to close it and redisplay...
5
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp The Joy of Interoperability Sometimes a revolution in programming forces you to abandon all...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
4
by: ARC | last post by:
Hello all, I didn't use to have this problem in Access 97, but in 2007 (and maybe other versions of access after 97), if you have a form that has a subform, and you click the close button, you...
16
by: Wayne | last post by:
I've read that one method of repairing a misbehaving database is to save all database objects as text and then rebuild them from the text files. I've used the following code posted by Lyle...
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: 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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.