Cod*_*ray 10
您需要覆盖表单的ProcessCmdKey方法.这是您通知子控件具有键盘焦点时发生的关键事件的唯一方式.
示例代码:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// look for the expected key
if (keyData == Keys.A)
{
// take some action
MessageBox.Show("The A key was pressed");
// eat the message to prevent it from being passed on
return true;
// (alternatively, return FALSE to allow the key event to be passed on)
}
// call the base class to handle other key events
return base.ProcessCmdKey(ref msg, keyData);
}
Run Code Online (Sandbox Code Playgroud)
至于为什么this.Focus()不起作用,这是因为形式本身不能成为焦点.特定控件必须具有焦点,因此当您将焦点设置到窗体时,它实际上将焦点设置为可以接受具有最低TabIndex值的焦点的第一个控件.在这种情况下,那是你的按钮.
| 归档时间: |
|
| 查看次数: |
9358 次 |
| 最近记录: |