C#: how to close form when esc key is pressed
-
hi my code is not working .can any one suggest me.
private void Form1_KeyPress(object sender, KeyPressEventArgs e) { if(e.KeyChar==(char)27) { this.Hide(); } }
ssk
You are hiding your form, not closing. If you want to close your form use
this.Close()
Also, if you have controls on the form, make sure that your form is processing key press until they reach the controls#region signature my articles #endregion
-
hi my code is not working .can any one suggest me.
private void Form1_KeyPress(object sender, KeyPressEventArgs e) { if(e.KeyChar==(char)27) { this.Hide(); } }
ssk
Hi! The simplest way is to assign the button your users are supposed to close the form (Cancel, Close, ...) to the
CancelButton
property of your form.Regards, mav -- Black holes are the places where God divided by 0...
-
Hi! The simplest way is to assign the button your users are supposed to close the form (Cancel, Close, ...) to the
CancelButton
property of your form.Regards, mav -- Black holes are the places where God divided by 0...
-
can u tell in detail. i am a beginner. i never used the Cancelbutton of a form thanks
senthil
You can add a button on the form and close the form when that button is clicked. Then in the properties of the form find Cancelbutton property and set it to the button which closes your form.
#region signature my articles #endregion
-
Hi! The simplest way is to assign the button your users are supposed to close the form (Cancel, Close, ...) to the
CancelButton
property of your form.Regards, mav -- Black holes are the places where God divided by 0...
-
ya its working. but one doubt, if i need this same task to be done without creating a button and setting its property to cancelbutton means , how ? dont think i am irritating u
senthil
Allowing the user to close a dialog by using the ESC key only is bad UI design style, so if you keep up a good style and provide a button you don't face the problem.
Regards, mav -- Black holes are the places where God divided by 0...