有没有办法抑制键盘蜂鸣声?

时间:2012-05-22 23:45:50

标签: c# winforms beep

我想在我的应用中抑制键盘蜂鸣声,或者至少在特定事件处理程序中。这可能吗?

更新

好的,你要求它(代码示例):

PREVIEW KEY DOWN:

    private void textBoxDuckbill_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
        switch (e.KeyCode) {
            case Keys.Down:
            case Keys.Up:
                e.IsInputKey = true;
                break;
        }
    }

KEY DOWN:

    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();
    }

3 个答案:

答案 0 :(得分:1)

我通过.NET 4.0 Winforms Code在ReSharper中进行了一些挖掘,我无法找到生成“嘟嘟”的位置。

这让我相信它超出了.NET的控制范围,并且没有阻止任何会引起哔哔声的键输入我不认为你可以抑制它。即使这样做也可能是不够的,因为您编写的任何处理程序可能会在导致发出蜂鸣声的代码后最终运行。

(左边有人点击这个寻找答案)

此外,这感觉你正在攻击错误的问题。我最好的猜测是你有一些操作导致你的应用程序发出很多嘟嘟声。而不是抑制蜂鸣声,为什么不确定是什么导致蜂鸣声并消除根部的问题?

编辑:

好的,我查看了背景颜色变化的代码。实际上没有理由改变这个属性会导致系统发出蜂鸣声。我甚至可以想象的 ONLY 是:

  1. 你有一个附加到.BackColorChanged的处理程序,它会引起哔哔声。
  2. 也许你的调试环境可能没有打破可能发生的异常。

      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"));
    

    这是控件上.BackColor的setter中的第一个代码块。基本上,如果您使用的表单样式(主题)不支持透明度,并且您提供的颜色在Alpha通道中具有除255之外的任何颜色,它将抛出异常。您的错误处理/调试环境可能会设置为不会抛出该异常,但是吞下并且系统发出哔声可能是一个指示。

  3. 这里有太多的变量可以给你一个确定的答案,但我强烈建议你从那里开始。 背景颜色真的没有理由会导致系统发出哔哔声。我几乎可以保证你这实际上是别的。

    这是其他事情的症状,简单地抑制哔哔声实际上可能隐藏可能导致其他地方出现其他错误的潜在问题。

    通常最好的做法是不要隐藏错误/异常,除非您针对特定内容制定了明确的行动方案。这就是为什么不鼓励盲目尝试/捕获,因为你会认为它是理所当然的,当另一个错误落入该陷阱时,你不会得到必要的调试信息来找到/修复它。

答案 1 :(得分:1)

您必须将密钥设置为已处理。

    e.Handled = true

或在某些情况下:

    e.SuppressKeyPress = true

编辑:没关系,OP已声明它不是无效按键发出哔哔声。

答案 2 :(得分:0)

我在KeyPress事件的底部更改了此代码:

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();
}

..它就像众所周知的魅力手链。 IOW,我不得不告诉它不允许输入密钥(尽管它是有效的),然后以编程方式将其放在那里。

为什么这样做或“必须这样”我不知道,但它有效,所以我或多或少满意。