抑制蜂鸣声 winform / 文本框
Suppressing a beep winform / textbox
我有一个键序列 "CTRL+U" 并且在表单中我有一个 KeyDown
方法来查看键变量 e
并执行它想要的操作。它没有设置结果。
有效。
但是如果表单的焦点在文本框中并且我按下组合键,它仍然有效,但随后我听到哔声。
我对如何解决这个问题感到有点困惑,因为在每个控件中都必须抑制一个键事件听起来需要做很多工作(我应该有几个文本框)。
负责人:
private void XXXXXForm_KeyDown(object sender, KeyEventArgs e)
{
if(e.Control)
{
bool bHandle = false;
bool bChecked = true;
if (e.KeyCode == Keys.U)
{
bChecked = false;
bHandle = true;
}
else if (e.KeyCode == Keys.T)
{
bChecked = true;
bHandle = true;
}
if(bHandle)
{
// Do stuff
}
}
}
我在另一个网站上找到了这个信息:
The "e.Handled = true;" statement is not doing what you think here.
The documentation [^]for this is confusing, and one could interpret it
the way you have. However, you need to realize that they are talking
about setting "Handled" in the KeyPress Event. To make matters worse,
the KeyPressed event uses KeyPressEventArgs not KeyEventArgs.
Instead use e.SuppressKeyPress = true;
这就是解决方案!
我有一个键序列 "CTRL+U" 并且在表单中我有一个 KeyDown
方法来查看键变量 e
并执行它想要的操作。它没有设置结果。
有效。
但是如果表单的焦点在文本框中并且我按下组合键,它仍然有效,但随后我听到哔声。
我对如何解决这个问题感到有点困惑,因为在每个控件中都必须抑制一个键事件听起来需要做很多工作(我应该有几个文本框)。
负责人:
private void XXXXXForm_KeyDown(object sender, KeyEventArgs e)
{
if(e.Control)
{
bool bHandle = false;
bool bChecked = true;
if (e.KeyCode == Keys.U)
{
bChecked = false;
bHandle = true;
}
else if (e.KeyCode == Keys.T)
{
bChecked = true;
bHandle = true;
}
if(bHandle)
{
// Do stuff
}
}
}
我在另一个网站上找到了这个信息:
The "e.Handled = true;" statement is not doing what you think here. The documentation [^]for this is confusing, and one could interpret it the way you have. However, you need to realize that they are talking about setting "Handled" in the KeyPress Event. To make matters worse, the KeyPressed event uses KeyPressEventArgs not KeyEventArgs.
Instead use e.SuppressKeyPress = true;
这就是解决方案!