简体   繁体   English

如何在EnterKeyPress上阻止NumericUpDown播放“叮”声

[英]How do I stop a NumericUpDown from playing 'Ding' sound on EnterKeyPress

I have a few NumericUpDown controls on my form, and everytime I hit the enter key while it is active, it plays this horrible DING noise, right in my ears. 我的窗体上有一些NumericUpDown控件,每次在它处于活动状态时按Enter键,它都会在我的耳朵上播放这种可怕的DING噪音。 It plays if I don't handle the KeyPress event, and it plays if I do handle the KeyPress event, with or without e.Handled = true : 如果我不处理KeyPress事件,它将播放;如果我处理KeyPress事件,则它将播放,无论是否带有e.Handled = true

myNumericUpDown.KeyPress += myNumericUpDown_KeyPress;

private void myNumericUpDown_KeyPress(object sender, EventArgs e)
{
    if (e.KeyChar == 13)
    {
        e.Handled = true; //adding / removing this has no effect
        myButton.PerformClick();
    }
}

And I don't think it is happening because I am handling it, as if I don't register the event (removing the first line above), it still plays the noise. 而且我不认为这是因为我正在处理它而发生,好像我没有注册该事件(删除上面的第一行)一样,它仍然在播放噪音。

Use KeyDown() and SuppressKeyPress, but click the button in KeyUp(): 使用KeyDown()和SuppressKeyPress,但是单击KeyUp()中的按钮:

    private void myNumericUpDown_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            e.Handled = e.SuppressKeyPress = true;
        }
    }

    private void myNumericUpDown_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            e.Handled = e.SuppressKeyPress = true;
            myButton.PerformClick();
        }
    }

*If you set the Forms AcceptButton Property to "myButton", then NO code is needed at all! *如果您设置窗体AcceptButton属性为“myButton的”时,则需要在所有NO代码!

Well, you are not supposed to use the Enter key like that in a GUI app. 好吧,您不应该像在GUI应用程序中那样使用Enter键。 It is reserved for another role. 它保留给另一个角色。 The DING! 丁! is an unsubtle reminder of that. 这是一个微妙的提醒。

You can easily unding it by actually giving the Enter key the intended usage. 您可以通过实际给Enter键指定预期的用途来轻松地对其进行注释。 Drop a button on the form, set its Visible property to False. 在窗体上放置一个按钮,将其Visible属性设置为False。 Select the form and set the AcceptButton property to that button. 选择窗体,然后将AcceptButton属性设置为该按钮。 No more dings. 没有更多的叮s声。

Extrapolating somewhat from the odd usage of Enter, you probably want to make the Enter key behave like it does in a console mode app, taking the user to the next control. 从Enter的奇怪用法中推断出一些内容,您可能希望使Enter键的行为像在控制台模式应用程序中一样,将用户带到下一个控件。 Normally done by using the Tab key instead. 通常使用Tab键代替。 You can accomplish that, and unding you UI, by copy/pasting this code into your form class: 您可以通过将以下代码复制/粘贴到表单类中来实现此目的,并展开UI:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        if (keyData == Keys.Enter) {
            var ctl = this.ActiveControl;
            var box = ctl as TextBoxBase;
            // Make Enter behave like Tab, unless it is a multiline textbox
            if (ctl != null && (box == null || !box.Multiline)) {
                this.SelectNextControl(ctl, true, true, true, true);
                return true;
            }
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

NumericUpDown also checks if the key you pressed is a "number key" (1-9,-,.), which is obviously not the case for the enter key. NumericUpDown还会检查您按下的键是否为“数字键”(1-9,-,.),而Enter键显然不是这种情况。 After you did what you wanted to do, just set e.KeyChar to any number key. 完成您想做的事情之后,只需将e.KeyChar设置为任何数字键。 Setting e.Handled = true is still necessary, otherwise the system treats your input as if you actually pressed that key instead and inserts the number. 仍然需要设置e.Handled = true ,否则系统会将您的输入视为您实际上按了该键,然后插入数字。

//Do something
e.Handled = true;
e.KeyChar = (char)Keys.D2;

For me, this worked perfectly. 对我来说,这很完美。

I do this trick: 我这样做:

Inside the numericUpandDown KeyPress function, if e.KeyChar=13 (Enter key), you have to set e.Handled=true; numericUpandDown KeyPress函数内部,如果e.KeyChar=13 (Enter键),则必须设置e.Handled=true; (that will prevent your control from changing it's value in any way) and just after that set e.KeyChar=(char)46; (这将阻止您的控件以任何方式更改其值),并在此之后立即set e.KeyChar=(char)46; (or any other numerical accepted value, it will not be written as e.Handled is already set to true). (或任何其他可接受的数字值,它将不会写为e.Handled已设置为true)。

This way, e.KeyChar has an accepted value by Visual Studio, and that "Ding" will not sound. 这样, e.KeyChar具有Visual Studio可接受的值,并且“ Ding”不会发出声音。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM