從屬性設置true的KeyPreview屬性;
添加keyDownEvent到窗體
在窗體的keyDownEvent,包括代碼
代碼
if(e.KeyValue==13)// When Enter Key is Pressed
{
// Last line is performing click. Other lines are making sure
// that user is not writing in a Text box
Control ct = userControl1 as Control;
ContainerControl cc = ct as ContainerControl;
if (!(cc.ActiveControl is TextBox))
validationButton.PerformClick(); // Code line to performClick
}
if(e.KeyValue==27) // When Escape Key is Pressed
{
// Last line is performing click. Other lines are making sure
// that user is not writing in a Text box
Control ct = userControl1 as Control;
ContainerControl cc = ct as ContainerControl;
if (!(cc.ActiveControl is TextBox))
cancelButton.PerformClick(); // Code line to performClick
}
以下行
validationButton或cancelButton是的名稱您按鈕,我只是假設。你可能有不同的。如果你有不同,使用你的名字而不是這兩個。
父窗體使用FindForm(),然後將其設置爲AcceptButton和CancelButton屬性。 – Arthur