[英]Why does e.Handled = true not stop key processing in this case?
I have this code.我有这个代码。 When I hit Enter when
toolFolderName
(a toolbar textbox) has focus, the Enter key is processed fine.当我在
toolFolderName
(工具栏文本框)具有焦点时按下 Enter 键时,Enter 键处理得很好。 Then, despite the e.Handled = true;
然后,尽管
e.Handled = true;
the filesDataGridView_KeyUp
event fires. filesDataGridView_KeyUp
事件触发。 Curiously (or maybe not, you decide), if I set a breakpoint in toolFolderName_KeyPress
, and hit Continue when execution breaks, without doing anything else, the keystroke does not propagate to the DGV event.奇怪的是(或者可能不是,你决定),如果我在
toolFolderName_KeyPress
设置断点,并在执行中断时点击继续,而不做任何其他事情,击键不会传播到 DGV 事件。
I realize I'm using both KeyUp
and KeyPress
events.我意识到我同时使用
KeyUp
和KeyPress
事件。 Does e.Handled
not work across those events? e.Handled
在这些事件中不起作用吗? I want a toolbar textbox and a DGV on the same form to use the Enter key, each for its own purposes.我希望同一个表单上的工具栏文本框和 DGV 使用 Enter 键,每个都用于自己的目的。 What approach should I take?
我应该采取什么方法?
private void toolFolderName_KeyPress(object sender, KeyPressEventArgs e)
{
if ((Keys)e.KeyChar == Keys.Return)
{
// code
// code
e.Handled = true;//does this not mean "Hey, every control on this form,
//ignore this keystroke, it has been handled?"
}
}
private void filesDataGridView_KeyUp(object sender, KeyEventArgs e)
{
// receives Enter key despite e.Handled in toolFolderName_KeyPress
if (e.KeyCode == Keys.Return)
{
// processing code
e.Handled = true;
}
}
e.Handled
only applies to this very event. e.Handled
只适用于这个事件。 If you want to suppress processing of KeyUp as well, you must use the same logic in the KeyUp handler or set a flag (most likely a bool
) in the KeyDown handler that tells the KeyUp handler to ignore its KeyUp event as well.如果您还想抑制 KeyUp 的处理,您必须在 KeyUp 处理程序中使用相同的逻辑或在 KeyDown 处理程序中设置一个标志(最有可能是
bool
),告诉 KeyUp 处理程序也忽略其 KeyUp 事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.