关闭Windows表单 [英] closing a windows form
本文介绍了关闭Windows表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何不使用鼠标单击而是使用C#中的键盘键(快捷键/加速键)来关闭活动的Windows窗体?
How can I close my active windows form without using mouse click, but by using keyboard keys (shortcut/accelerated keys) in C#?
推荐答案
您只需要订阅KeyDown事件处理程序,然后在按下某个键时会收到一个事件.在这种情况下,您可以检查按下了哪个键.例如,如果要在按下返回"时关闭表单,则可以使用:
You just need to subscribe the KeyDown event handler and then you will get an event when a key is pressed. In this event you can check which key was pressed. If for example you want to close your form when "Return" is pressed, then you might use:
private void KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
this.Close();
}
在页面Keydown或keyup事件中,应编写如下代码,
对于VB,
In Page Keydown or keyup event write code like this,
For VB,
If e.KeyCode = Keys.Esc Then
Me.close()
End If
对于C#,
For C#,
this.Close();
只需使用您的表单keydown事件即可.
Hi,
Just use your form keydown event.
private void form_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Escape)
{
// use message box if you need.
this.close();
}
}
干杯:)
Cheers :)
这篇关于关闭Windows表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文