Stop "Dean" by pressing Enter

I have a very simple Windows Forms application. And on Windows (or at least Windows Forms applications), when you press Enter while inside a single-line TextBox control, you hear Ding. This is an unpleasant sound, indicating that you cannot enter a new line, because it is a single-line TextBox.

Everything is fine. However, in my form I have 1 TextBox and a search button. And I allow the user to search by pressing Enter after they have typed, so they don’t need to use the mouse to click the Search button.

But that Dean sound arises. This is very annoying.

How can we make sure that the sound does not play at all in my form?

@David H - Here, as I detect input press:

private void textBox1_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { // Perform search now. } } 
+111
c # winforms audio
Jun 09 '11 at 9:50 a.m.
source share
12 answers

Check the Form.AcceptButton property. You can use it to indicate the default button for the form, in this case, to press enter.

From the docs:

This property allows you to set the default action to be performed when the user presses the ENTER key in your expression. The button assigned to this property must be an IButtonControl that is on the current form or is inside the container on the current form.

There is also a CancelButton property when the user presses the escape button.

+52
Jun 09 2018-11-11T00:
source share

This works for me:

 private void textBox1_KeyDown(object sender, KeyEventArgs e) { //Se apertou o enter if (e.KeyCode == Keys.Enter) { //enter key is down this.doSomething(); e.Handled = true; e.SuppressKeyPress = true; } } 

The SuppressKeyPress is really a trick. Hope this helps you.

+178
May 3 '13 at 3:02
source share

Try

 textBox.KeyPress += new KeyPressEventHandler(keypressed); private void keypressed(Object o, KeyPressEventArgs e) { if (e.KeyCode == Keys.Enter) { e.Handled = true; //this line will do the trick } } 
+52
Jun 09 2018-11-11T00:
source share

You can use KeyPress instead of KeyUp or KeyDown more efficiently and here, how to handle

  private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { e.Handled = true; button1.PerformClick(); } } 

and say the world "Dean"

+11
Dec 17 '14 at 21:17
source share

Just add e.SuppressKeyPress = true; into your if statement.

 private void textBox1_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { //If true, do not pass the key event to the underlying control. e.SuppressKeyPress = true; //This will suppress the "ding" sound.*/ // Perform search now. } } 
+11
Jul 10 '15 at 17:53 on
source share

Use SuppressKeyPress to stop the continuous processing of a keystroke after it has been processed.

 public class EntryForm: Form { public EntryForm() { } private void EntryTextBox_KeyDown(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Enter) { e.Handled = true; e.SuppressKeyPress = true; // do some stuff } else if(e.KeyCode == Keys.Escape) { e.Handled = true; e.SuppressKeyPress = true; // do some stuff } } private void EntryTextBox_KeyUp(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Enter) { // do some stuff } else if(e.KeyCode == Keys.Escape) { // do some stuff } } } 
+7
Sep 15 '15 at 7:59
source share

I stumbled upon this post while trying to process KeyDown, it worked for me.

 If e.KeyCode = Keys.Enter Then e.SuppressKeyPress = True btnLogIn.PerformClick() End If 

Suppressing a keystroke stops the event from being sent to the base control. This should work if you manually process everything that the input key will do in this text box. Sorry for Visual Basic.

+2
Apr 26 '13 at 14:43
source share

There is very little chance that anyone will get to this answer, but some other answers are really scary. Suppressing an event on KeyDown kills 2 additional events in one hit. In this context, the e.Handled property for true useless.
The best way is to set the Form.AcceptButton property to the actual search button.
There is also another way to use the Enter key - some people may want it to act as a TAB button. To do this, add a new Button , set its Location property outside the Form (i.e. (-100, -100) ) - setting the Visible property to false can disable Button handlers in some cases. Set the Form.AcceptButton property Form.AcceptButton new button. In the Click event handler, add the following code
this.SelectNextControl(ActiveControl, true, true, true, true)

Now you may need to pass focus only when focus on a TextBox may need to either test the ActiveControl type or use the e.Supress property in control event handlers that are not intended to use Enter as TAB This. You don’t even have to grab e.KeyCode

+2
Feb 25 '16 at 22:55
source share
 $("#txtSomething").keypress(function (e) { if (e.which == 13) { e.Handled = true; //This will prevent the "ding" sound //Write the rest of your code } }); 
+1
Nov 13 '13 at 0:58
source share

Set the IsDefault search button to true . This will make it the default button, and when you press Enter, the button will be automatically pressed.

0
Jun 09 2018-11-11T00:
source share

Well, I lived with this problem for a long time and looked at it here.

After thinking about this for quite some time and wanting the easiest way to fix it, I came up with the easiest, but not very elegant way to fix it.

Here is what I did.

  • Place the two invisible buttons "OK" and "Cancel" on the form.
  • Set the AcceptButton and CancelButton properties on the form to invisible buttons.
  • Added code for buttons!

This resolved all the secondary problems listed in this thread, including ToolStripMenu. My biggest complaint was the BindingNavigator when I entered the record number at the current position to go and press enter.

According to the original question, in which the programmer wanted the search function when pressing the enter button, I just put the search code in an invisible OK button!

So far this seems to solve all the problems, but as we all know with Visual Studio, something is likely to come up.

The only elegant way that I could think of is to write a new keystroke handling class that allows me to work a lot for most of my projects.

0
Apr 20 '15 at 1:29
source share
 void RTextBox_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == Keys.Enter) { //do ... bool temp = Multiline; Multiline = true; e.Handled = true; Multiline = temp; } } 
-2
Mar 02 '13 at 15:44
source share



All Articles