Reputation: 4006
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. 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
:
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.
Upvotes: 4
Views: 2519
Reputation: 11
The problem with any of these Key event solutions that prevent the NumericUpDown from seeing the ENTER key is that ENTER has the desirable action of causing the ValueChanged event. Important when the user types in the control and hits ENTER to commit the value.
My solution is to put a dummy button on the form and set it as the 'AcceptButton' to silence the DING. It has to be Visible and Enabled. And we need to set TabStop to false and hide it below another control so the user cannot interact with it directly. Fortunately the button doesn't steal focus and we can use that to test which control on the form the ENTER key belongs to in the button's Click event, where the NumericUpDown also, fortunately, reports it's value as the typed text.
private void buttonDummy_Click(object sender, EventArgs e)
{
if (numericUpDownCtrl.Focused)
numericUpDownCtrl_ValueChanged(null, EventArgs.Empty);
}
Upvotes: 0
Reputation: 11
I do this trick:
Inside the numericUpandDown
KeyPress function, if e.KeyChar=13
(Enter key), you have to set 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;
(or any other numerical accepted value, it will not be written as e.Handled
is already set to true).
This way, e.KeyChar
has an accepted value by Visual Studio, and that "Ding" will not sound.
Upvotes: 1
Reputation: 41
NumericUpDown also checks if the key you pressed is a "number key" (1-9,-,.), which is obviously not the case for the enter key. After you did what you wanted to do, just set e.KeyChar
to any number key. 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.
//Do something
e.Handled = true;
e.KeyChar = (char)Keys.D2;
For me, this worked perfectly.
Upvotes: 4
Reputation: 941397
Well, you are not supposed to use the Enter key like that in a GUI app. 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. Drop a button on the form, set its Visible property to False. Select the form and set the AcceptButton property to that button. No more dings.
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. Normally done by using the Tab key instead. You can accomplish that, and unding you UI, by copy/pasting this code into your form class:
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);
}
Upvotes: 2
Reputation: 39122
Use KeyDown() and SuppressKeyPress, but click the button in 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!
Upvotes: 5