gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 03:37:38 26 4
gpt4 key购买 nike

我尝试了以下方法:

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

但它不起作用。

然后我试了一下:

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

仍然没有任何效果。

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

最佳答案

这将始终有效,无论适当的事件处理程序分配、KeyPreviewCancelButton 等:

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

关于c# - 退出按钮以关闭 C# 中的 Windows 窗体窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2290959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com