退出按钮以关闭 C# 中的 Windows 窗体窗体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2290959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:07:49  来源:igfitidea点击:

Escape button to close Windows Forms form in C#

c#winforms

提问by yeahumok

I have tried the following:

我尝试了以下方法:

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    if ((Keys) e.KeyValue == Keys.Escape)
        this.Close();
}

But it doesn't work.

但它不起作用。

Then I tried this:

然后我尝试了这个:

protected override void OnKeyDown(KeyEventArgs e)
{
    base.OnKeyDown(e);
    if (e.KeyCode == Keys.Escape)
        this.Close();
}

And still nothing's working.

仍然没有任何效果。

The KeyPreview on my Windows Forms form properties is set to true... What am I doing wrong?

我的 Windows 窗体窗体属性上的 KeyPreview 设置为 true...我做错了什么?

采纳答案by Hans Passant

This will always work, regardless of proper event handler assignment, KeyPreview, CancelButton etc:

无论是否正确分配事件处理程序、KeyPreview、CancelButton 等,这都将始终有效:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        if (keyData == Keys.Escape) {
            this.Close();
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

回答by Shawn Steward

You should just be able to set the Form's CancelButtonproperty to your Cancel button and then you won't need any code.

您应该能够将 Form 的CancelButton属性设置为您的 Cancel 按钮,然后您就不需要任何代码了。

回答by Adam Robinson

Assuming that you have a "Cancel" button, setting the form's CancelButtonproperty (either in the designer or in code) should take care of this automatically. Just place the code to close in the Clickevent of the button.

假设您有一个“取消”按钮,设置表单的CancelButton属性(在设计器中或在代码中)应该会自动处理。只需将代码放置Click在按钮事件中即可关闭。

回答by SwDevMan81

By Escape button do you mean the Escape key? Judging by your code I think that's what you want. You could also try Application.Exit(), but Close should work. Do you have a worker thread? If a non-background thread is running this could keep the application open.

Escape 按钮是指 Escape 键吗?从你的代码来看,我认为这就是你想要的。您也可以尝试 Application.Exit(),但 Close 应该可以工作。你有工作线程吗?如果非后台线程正在运行,这可能会使应用程序保持打开状态。

回答by Joel

The accepted answer indeed is correct, and I've used that approach several times. Suddenly, it would not work anymore, so I found it strange. Mostly because my breakpoint would not be hit for ESCkey, but it would hit for other keys.

接受的答案确实是正确的,我已经多次使用这种方法。突然,它不再起作用了,所以我觉得很奇怪。主要是因为我的断点不会被击中ESC键,但会击中其他键。

After debugging I found out that one of the controls from my form was overriding ProcessCmdKeymethod, with this code:

调试后,我发现表单中的一个控件是覆盖ProcessCmdKey方法,代码如下:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    // ...
    if (keyData == (Keys.Escape))
    {
        Close();
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

... and this was preventing my form from getting the ESCkey (notice the return true). So make sure that no child controls are taking over your input.

...这阻止了我的表单获取ESC密钥(注意return true)。因此,请确保没有子控件接管您的输入。

回答by Dawid Bobyla

You need add this to event "KeyUp".

您需要将此添加到事件“KeyUp”中。

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

回答by Kristian

You set KeyPreview to true in your form options and then you add the Keypress event to it. In your keypress event you type the following:

您在表单选项中将 KeyPreview 设置为 true,然后向其中添加 Keypress 事件。在您的按键事件中,您键入以下内容:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == 27)
    {
        Close();
    }
}

key.Char == 27is the value of escape in ASCII code.

key.Char == 27是 ASCII 码中的转义值。

回答by webMac

You can also Trigger some other form.

你也可以触发一些其他的形式。

E.g. trigger a Cancel-Button if you edit the Form CancelButton property and set the button Cancel.

例如,如果您编辑 Form CancelButton 属性并将按钮设置为 Cancel,则触发 Cancel-Button。

In the code you treath the Cancel Button as follows to close the form:

在您按如下方式处理取消按钮以关闭表单的代码中:

    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Abort;
    }