[英]Closing mdi child with esc
I need to close an mdi child
with Esc key. 我需要用Esc键关闭一个mdi child
。 I tried using keydown
and keypress
events, but i cant even get the form respond to those events when pressing any key. 我尝试使用keydown
和keypress
事件,但是当按任意键时我甚至无法让表单响应这些事件。
try this 尝试这个
private void Form_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
}
or make use of 或利用
Form.CancelButton Property
- Gets or sets the button control that is clicked when the user presses the ESC key. Form.CancelButton Property
- 获取或设置用户按下ESC键时单击的按钮控件。
Set the Property of the Form KeyPreview=True
and go with Keydown Event
设置Form KeyPreview=True
的属性并使用Keydown Event
if (e.KeyCode == Keys.Escape){
this.Close();
}
If your MDI Form has Close
button, then you can assign CancelButton
property with the ID of Close
button in your Form. 如果您的MDI表单有“ Close
按钮,则可以在表单中为“ Close
按钮分配“ CancelButton
Close
按钮属性。
So when you press ESC key, it will call Close
button click. 因此,当您按ESC键时,它将调用Close
按钮单击。
More : CancelButton 更多: CancelButton
First you must set the Form.KeyPreview = true
and you must know what is the Difference between the KeyUp and KeyDown Event 首先,您必须设置Form.KeyPreview = true
,您必须知道KeyUp和KeyDown事件之间的区别是什么
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
}
and if you want the KeyPress Event 如果你想要KeyPress事件
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 27)
{
this.Close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.