" ESC"关闭申请的关键 [英] "ESC" key to close application

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

问题描述

我在C#Visual Studio 2012中开发了一个Windows窗体应用程序。我想按ESC键关闭应用程序。我怀疑应该使用哪个按键事件? Form_KeyPress或textBox_KeyPress?

I have developing an Windows Form application in C# Visual Studio 2012.I want to close the application by pressing "ESC" key.My doubt is which keypress event should I use? Form_KeyPress or textBox_KeyPress?

推荐答案

如果您将Form的'KeyPreview属性设置为'true,那么它将在任何Form控件获得Key事件之前处理Key事件。



我强烈建议......因为你正在做一些不太标准的Win UI设计...你建立了一个安全机制,所以用户不会取消应用程序......可能会丢失数据:
If you set the Form's 'KeyPreview Property to 'true, then it will handle a Key Event before any of the Form's Controls get the Key Event.

I would strongly suggest ... since you are doing something which is not quite standard Win UI design ... that you build in a safety mechanism so the user doesn't cancel the Application ... possibly losing data:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape)
    {
        if 
        (
            MessageBox.Show
            (
                "Quit the Application",
                "Exit Application Dialog",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning,
                MessageBoxDefaultButton.Button2 // hit Enter == No !
            )
            == DialogResult.Yes
        )
        {
           Application.Exit();
        }
    }
}

我更喜欢使用KeyUp事件。在真实世界的代码中,我将实现与用户是否已保存其工作相关的某种级别的警告!

I prefer to use the KeyUp Event. In "real-world code" I'd would be implementing some level of warning related to whether or not the user had saved their work !


我想tou应该使用表格 ProcessKeyPreview [ ^ ]。
I suppose tou should use the Form ProcessKeyPreview[^].


我建​​议这是一个实验的好时机,看看会发生什么。



如果文本框没有焦点怎么办 - 你的表格会退出吗?



这会导致你做出了正确的决定。
I would suggest that this is a good time to experiment and see what happens.

What if the textbox doesn't have focus - does your form exit?

This will lead you to the correct decision.


这篇关于" ESC"关闭申请的关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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