逃生时关闭表格 [英] Close form on escape

查看:109
本文介绍了逃生时关闭表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在退出按钮按下事件时关闭了表单.

如何做到这一点?

任何帮助将不胜感激.

预先谢谢您.

解决方案

在表单的key_up事件中编写以下代码:

 私有 无效 frmFeeReceipts_KeyUp(对象发​​件人,KeyEventArgs e)
        {
            如果(例如,KeyCode == Keys.Escape)
                 .Close();
        } 


另外,请查看以下有关CP的文章,以了解一些替代方法:
在按Escape键时关闭应用程序 [ ^ ]
在按Escape键时关闭应用程序 [ ^ ]


1.处理表单的KeyUp 事件,并在其中添加以下代码:


 私有  void  Form1_KeyUp(对象发​​件人,KeyEventArgs e)
        {
            如果(例如,KeyCode == Keys.Escape)
            {
                 .Close();
            }
        } 


Prasad_Kulkarni 的Solutin 1和 Rahul Rajat Singh 的Solution 2都很好.
我想补充一点,当按下一个键时,它将转到窗体上具有focus control .假设光标位于TextBox中.现在,如果按下Escape key,它将不会进入Form''s KeyUp事件,而是由TextBox 捕获,并且触发TextBox KeyUp事件.

因此,要启用Form KeyUp 事件,即使将焦点放在任何其他控件上,也必须将Form KeyPreview 属性设置为true ,如此处所述
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx [ ^ ]
获取或设置一个值,该值指示在事件传递到具有焦点的控件之前,表单是否将接收键事件.
并注明为
要仅在表单级别处理键盘事件,并且不允许控件接收键盘事件,请将表单的KeyPress事件处理程序中的KeyPressEventArgs.Handled属性设置为true.

设置KeyPreview property后,将首先触发Form''s KeyUp事件,然后将触发具有焦点的控件的KeyUp 事件.


I wnat to close my form on escape button pressed event..

how to achieve this?

any help would be appreciated.

Thank you in advance.

解决方案

On key_up event of your form write this code:

private void frmFeeReceipts_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }


And also, check following articles on CP for some alternatives:
Close Application at Press of Escape Button[^]
Close Application at Press of Escape Button[^]


1. Handle the KeyUp event for the form and put following code in it:


private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }


The Solutin 1 by Prasad_Kulkarni and Solution 2 by Rahul Rajat Singh are good.

I want to add that when a key is pressed it will go to the control which has focus on the form. Let us say the cursor is in a TextBox. Now, if the Escape key is pressed it will not go to the Form''s KeyUp event and instead it is captured by the TextBox and the KeyUp event of TextBox fires.

So, to enable the KeyUp event of Form even when the focus is on any other control, then the KeyPreview property of the Form has to be set to true as explained here
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx[^]
Gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.
and under remarks as
To handle keyboard events only at the form level and not allow controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form''s KeyPress event handler to true.

After setting KeyPreview property the Form''s KeyUp event will fire first, then the KeyUp event of the control which has focus will be fired.


这篇关于逃生时关闭表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆