Stop the 'Ding' when pressing Enter

帅比萌擦擦* 提交于 2019-11-26 17:38:57
mdm

Check out the Form.AcceptButton property. You can use it to specify a default button for a form, in this case for pressing enter.

From the docs:

This property enables you to designate a default action to occur when the user presses the ENTER key in your application. The button assigned to this property must be an IButtonControl that is on the current form or located within a container on the current form.

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

It 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 the really trick. I hope that help you.

FIre Panda

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
    }
}

You can Use KeyPress instead of KeyUp or KeyDown its more efficient and here's how to handle

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

and say peace to the 'Ding'

brandonstrong

Just add e.SuppressKeyPress = true; in 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.
    }
}
Snehasish

Use SuppressKeyPress to stop continued processing of the keystroke after handling it.

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

      }
   }
}

I stumbled on this post while trying to handle a KeyDown this worked for me.

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

Supressing the Key Press stops the event from being sent to the underlying control. This should work if you're manually handling everything that the enter key will be doing within that textbox. Sorry about the Visual Basic.

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

Now, you may want to transfer focus only when focus it on TextBox you may want to either test ActiveControl type or use e.Supress property in event handlers of controls not meant to use Enter as TAB That's it. You don't even need to capture e.KeyCode

$("#txtSomething").keypress(function (e) {
        if (e.which == 13) {

            e.Handled = true; //This will prevent the "ding" sound

            //Write the rest of your code
        }
    });

Set your Search button's IsDefault property to true. This will make it a default button and it will be auto-clicked when Enter is pressed.

Well I lived with this problem long enough and looked it up here.

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

Here is what I did.

  1. Put 2 invisible buttons "Ok" and "Cancel" on the form.
  2. Set the AcceptButton and CancelButton Property on the form to the invisible buttons.
  3. Added no code to the buttons!

This solved all the secondary problems listed in this thread including the ToolStripMenu. My biggest complaint was the BindingNavigator, when I would enter a record number into the Current position to navigate to and pressed enter.

As per the original question in which the programmer wanted a search function when the enter button was pressed I simply put the search code in the invisible OK Button!

So far this seems to solve all problems but as we all know with Visual Studio, something will probably crop up.

The only other possible elegant way I could think of would be to write a new keystroke handling class which is way to much work for most of my projects.

mahdi
void RTextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyData == Keys.Enter)
    {
        //do ...
        bool temp = Multiline;
        Multiline = true;
        e.Handled = true;
        Multiline = temp;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!