0

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

更新

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

预览键向下:

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

3 回答 3

1

我通过 .NET 4.0 Winforms 代码对 ReSharper 进行了一些挖掘,但我无法找到生成“哔”的位置。

这使我相信它超出了 .NET 的控制范围,并且无法阻止任何会导致哔哔声的键输入,我认为您无法抑制它。即使这样做可能还不够,因为您编写的任何处理程序来阻止它实际上可能最终在导致哔声的代码之后运行。

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

此外,这感觉就像你在攻击错误的问题。我最好的猜测是你有一些操作导致你的应用程序发出很多哔声。与其抑制蜂鸣声,不如确定导致蜂鸣声的原因并从根本上消除问题?

编辑:

好的,我已经查看了背景颜色更改的代码。确实没有理由更改此属性会导致系统发出哔哔声。我能想象的唯一事情是:

  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控件设置器中的第一个代码块。基本上,如果您使用的表单样式(主题)不支持透明度,并且您提供的颜色在 alpha 通道中具有 255 以外的任何颜色,则会引发异常。可能您的错误处理/调试环境的设置方式不会引发该异常,但会被吞下,并且系统蜂鸣声可能是一个指标。

这里的变量实在太多,无法给你一个明确的答案,但我强烈建议你从那里开始。背景颜色确实没有理由引起系统蜂鸣声。我几乎可以向你保证,这实际上是另一回事。

这是其他问题的症状,简单地抑制哔声实际上可以隐藏一个潜在的问题,该问题可能会导致其他地方的其他错误。

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

于 2012-05-22T23:56:45.740 回答
1

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

    e.Handled = true

或在某些情况下:

    e.SuppressKeyPress = true

编辑:没关系,OP已经声明这不是无效的按键蜂鸣声。

于 2012-05-23T00:00:27.207 回答
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,我不得不告诉它不允许输入密钥(尽管它是有效的),然后以编程方式将其放在那里。

为什么这行得通或“必须这样”我不知道,但它行得通,所以我或多或少地感到满意。

于 2012-05-24T22:36:25.990 回答