C# 有没有办法抑制键盘的哔哔声?
我想在我的应用程序中,或者至少在特定事件处理程序中,抑制键盘的蜂鸣音。这可能吗 更新 好的,您要求它(代码示例): 向下预览键:C# 有没有办法抑制键盘的哔哔声?,c#,winforms,beep,C#,Winforms,Beep,我想在我的应用程序中,或者至少在特定事件处理程序中,抑制键盘的蜂鸣音。这可能吗 更新 好的,您要求它(代码示例): 向下预览键: private void textBoxDuckbill_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { switch (e.KeyCode) { case Keys.Down: case Keys.Up:
private void textBoxDuckbill_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
switch (e.KeyCode) {
case Keys.Down:
case Keys.Up:
e.IsInputKey = true;
break;
}
}
按下键:
private void textBoxDuckbill_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
switch (e.KeyCode) {
case Keys.Down:
case Keys.Up:
e.IsInputKey = true;
break;
}
}
private void textBoxDuckbill_KeyDown(object sender, KeyEventArgs e) {
TextBox tb = (TextBox)sender;
if (e.KeyCode.Equals(Keys.Up)) {
SetFocusOneRowUp(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Down)) {
SetFocusOneRowDown(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Left)) {
SetFocusOneColumnBack(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Right)) {
SetFocusOneColumnForward(tb.Name);
e.Handled = true;
return;
}
}
按键:
private void textBoxDuckbill_KeyPress(object sender, KeyPressEventArgs e) {
TextBox tb = (TextBox)sender;
errorProviderCRLogins.SetError(tb, String.Empty);
// If user presses "%" (37) move back/left one TextBox column;
// if user presses "'"(39) move forward/right one TextBox column.
// Also now allowing navigational arrows to do the same thing (KeyDown event)
if (e.KeyChar == '%') {
SetFocusOneColumnBack(tb.Name);
e.Handled = true;
return;
}
if (e.KeyChar == Convert.ToChar(@"'")) {
SetFocusOneColumnForward(tb.Name);
e.Handled = true;
return;
}
// Preclude values (1,2,3) that would normally be allowed (see below) but do
// not have a value in the corresponding PlatypusID TextBox
if (((e.KeyChar == '1') && (String.IsNullOrWhiteSpace(textBoxPlatypusNum1.Text))) ||
((e.KeyChar == '2') && (String.IsNullOrWhiteSpace(textBoxPlatypusNum2.Text))) ||
((e.KeyChar == '3') && (String.IsNullOrWhiteSpace(textBoxPlatypusNum3.Text)))) {
e.Handled = true;
return;
}
// Now, having gotten to here, we can assume that 1, 2, and 3 are valid (as are
// Space and Backspace all the time).
if ((e.KeyChar != '1') &&
(e.KeyChar != '2') &&
(e.KeyChar != '3') &&
(e.KeyChar != (char)Keys.Space) &&
(e.KeyChar != (char)Keys.Back)) {
e.Handled = true;
return;
}
// Added Space as an allowable entry so user can delete a val with that key
// (which will automatically happen on tabbing into the TextBox, as it is
// now being highlighted)
if ((e.KeyChar == (char)Keys.Space) || (e.KeyChar == (char)Keys.Back)) {
tb.Text = String.Empty;
buttonSave.Enabled = true;
// Don't return here, as they might continue to hit Space to zero out
// subsequent cells
}
// Now, if there is already a value in the cell (this is a repeated val, as shown
// by TextLength being 1 instead of 0), move it to the next cell and give it the
// value just entered (even if space for "delete")
if ((tb.TextLength == 1) || (e.KeyChar == (char)Keys.Space)) {
buttonSave.Enabled = true;
MoveToNextCellAndEnterVal(e.KeyChar.ToString(), tb.Name);
}
// Although KeyChar has a val such as 49/("1"), TextLength == 0
if ((e.KeyChar == '1') ||
(e.KeyChar == '2') ||
(e.KeyChar == '3')) {
buttonSave.Enabled = true;
}
}
文本更改:
private void textBoxDuckbill_TextChanged(object sender, EventArgs e) {
TextBox tb = (TextBox)sender;
if (tb.Text == "1") {
tb.BackColor = PlatypusID1_BACKCOLOR;
tb.ForeColor = PlatypusID1_FORECOLOR;
return;
}
if (tb.Text == "2") {
tb.BackColor = PlatypusID2_BACKCOLOR;
tb.ForeColor = PlatypusID2_FORECOLOR;
return;
}
if (tb.Text == "3") {
tb.BackColor = PlatypusID3_BACKCOLOR;
tb.ForeColor = PlatypusID3_FORECOLOR;
return;
}
tb.BackColor = System.Drawing.SystemColors.Window;
tb.ForeColor = System.Drawing.SystemColors.WindowText;
}
private void MoveToNextCellAndEnterVal(string APlatypusID, string ATextBoxName) {
String numericPortionOfTextBoxName = ATextBoxName.Remove(0, LENGTH_OF_TEXT_BOX_BASE);
String sTextBoxToFind;
int textBoxNumber = 0;
int nextTextBoxNumber;
int.TryParse(numericPortionOfTextBoxName, out textBoxNumber);
nextTextBoxNumber = ++textBoxNumber;
// "wrap around"
if (nextTextBoxNumber > NUMBER_OF_QUARTER_HOURS) {
nextTextBoxNumber = nextTextBoxNumber - NUMBER_OF_QUARTER_HOURS;
}
sTextBoxToFind = String.Format("textBoxDuckbill{0}", nextTextBoxNumber);
TextBox tb = (TextBox)this.Controls.Find(sTextBoxToFind, true).First();
tb.Focus();
tb.Text = APlatypusID;
}
private void SetFocusOneRowDown(string ATextBoxName) {
String numericPortionOfTextBoxName = ATextBoxName.Remove(0, LENGTH_OF_TEXT_BOX_BASE);
String sTextBoxToFind;
int textBoxNumber = 0;
int nextTextBoxNumber;
int.TryParse(numericPortionOfTextBoxName, out textBoxNumber);
if (!(textBoxNumber == NUMBER_OF_QUARTER_HOURS)) {
nextTextBoxNumber = ++textBoxNumber;
} else {
nextTextBoxNumber = 1;
}
sTextBoxToFind = String.Format("textBoxDuckbill{0}", nextTextBoxNumber);
TextBox tb = (TextBox)this.Controls.Find(sTextBoxToFind, true).First();
tb.Focus();
}
我通过.NET4.0WinForms代码在ReSharper中做了一点挖掘,没有找到生成“嘟嘟声”的位置
这让我相信它超出了.NET的控制范围,并且没有阻止任何可能导致哔哔声的按键输入,我认为您无法抑制它。即使这样做也可能不够,因为您为停止此操作而编写的任何处理程序实际上都可能在导致哔哔声的代码之后运行
(左图,以防有人点击此处寻找答案)
而且,这让人感觉你在攻击错误的问题。我最好的猜测是你有一些操作导致你的应用程序发出很多嘟嘟声。与其抑制蜂鸣音,为什么不确定是什么引起蜂鸣音并从根本上消除问题
编辑:
好的,我已经看了背景颜色改变的代码。没有理由更改此属性会导致系统发出嘟嘟声。我唯一能想象的是:
的某个位置附加了一个处理程序。BackColorChanged
正在发出嘟嘟声 if (!value.Equals((object) System.Drawing.Color.Empty) && !this.GetStyle(ControlStyles.SupportsTransparentBackColor) && (int) value.A < (int) byte.MaxValue)
throw new ArgumentException(System.Windows.Forms.SR.GetString("TransparentBackColorNotAllowed"));
如果(!value.Equals((object)System.Drawing.Color.Empty)和&!this.GetStyle(ControlStyles.supportsRasparentBackColor)和&(int)value.A<(int)byte.MaxValue)
抛出新ArgumentException(System.Windows.Forms.SR.GetString(“TransparentBackColorNotAllowed”);
这是控件上.BackColor
的setter中的第一个代码块。基本上,如果您使用的表单样式(主题)不支持透明度,并且您提供的颜色在alpha通道中有255以外的颜色,则会引发异常。您的错误处理/调试环境的设置方式可能不会引发异常,但会被吞没,并且系统蜂鸣声可能是一个指示器通常最好的做法是不隐藏错误/异常,除非您对某些特定的事情有明确的行动方针。这就是为什么不鼓励盲目尝试/捕获的原因,因为您认为这是理所当然的,当另一个错误落入该陷阱时,您将无法获得必要的调试信息来查找/修复它。您必须将密钥设置为已处理
e.Handled = true
或者在某些情况下:
e.SuppressKeyPress = true
编辑:没关系,OP声明这不是无效的按键声。一旦我在按键事件底部更改了此代码:
if ((e.KeyChar == '1') ||
(e.KeyChar == '2') ||
(e.KeyChar == '3')) {
buttonSave.Enabled = true;
}
……为此:
if ((e.KeyChar == '1') ||
(e.KeyChar == '2') ||
(e.KeyChar == '3')) {
buttonSave.Enabled = true;
e.Handled = true;
tb.Text = e.KeyChar.ToString();
}
…它就像众所周知的魅力手镯。我必须告诉它不要输入密钥(尽管它是有效的),然后按程序将它放在那里
我不知道为什么这样做或“必须这样做”,但它是有效的,所以我或多或少感到满意。我假设你是指当你试图在一个空输入框中单击backspace时发出的蜂鸣音?实际上,每当新值与旧值不同时,它就会发出蜂鸣音。这样做的目的是将光标移动到下一个文本框(我的代码就是这样做的),并更改文本框的颜色(同样,我的代码)。但是它只有在值/颜色与以前不同时才会发出嘟嘟声…?你有一些代码示例给我们看吗?@Aren:好的,你要求的-上面的代码在更新部分之后。这显然是我更改文本框的背景颜色时引起的。我也对抑制这件事感兴趣,WinXP与Win7不同。相关:如果我抑制它或处理它,用户输入的值不会输入到文本框中,因此这对我来说不是一个开始。