144

我有一个非常简单的 Windows 窗体应用程序。而且,在 Windows(或至少是 Windows 窗体应用程序)中,当您在单行文本框控件中按 Enter 时,您会听到叮叮声。这是一个令人不快的声音,表明您不能输入换行符,因为它是一个单行文本框。

这一切都很好。但是,在我的表单中,我有 1 个文本框和一个搜索按钮。而且我允许用户在完成输入后通过按 Enter 来执行搜索,因此他们不必使用鼠标单击搜索按钮。

但这丁的声音出现了。这很烦人。

我们怎样才能让它在我的表单中根本不播放声音?

@David H - 这是我检测输入的方式:

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        // Perform search now.
    }
}
4

16 回答 16

237

这个对我有用:

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;

     }

 }

SuppressKeyPress 是真正的诀窍。我希望对你有所帮助。

于 2013-05-03T03:02:34.787 回答
63

查看Form.AcceptButton属性。您可以使用它为表单指定默认按钮,在本例中为按 Enter。

从文档:

此属性使您能够指定当用户在应用程序中按下 ENTER 键时发生的默认操作。分配给此属性的按钮必须是当前窗体上或位于当前窗体上的容器内的 IButtonControl。

当用户按下转义键时,还有一个CancelButton属性。

于 2011-06-09T09:55:03.790 回答
57

尝试

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
    }
}
于 2011-06-09T09:54:25.543 回答
17

只需添加e.SuppressKeyPress = true;您的“if”语句。

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.
    }
}
于 2015-07-10T17:53:53.017 回答
15

您可以使用 KeyPress 而不是 KeyUp 或 KeyDown 它更有效,这里是如何处理

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

和'丁'说和平

于 2014-12-17T21:17:14.133 回答
9

用于SuppressKeyPress在处理击键后停止继续处理击键。

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

      }
   }
}
于 2015-09-15T07:59:51.373 回答
4

在 WinForms 上,Enter 键会导致 Ding 声音,因为未指定表单属性 AcceptButton。如果您不需要 AcceptButton,则可以通过将表单 KeyPreview 设置为 true 并输入以下 KeyPress 事件来抑制叮当声:

private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\r')
        e.Handled = true;
}

无论激活哪个控件,按下 Enter 键时都不会再发出叮当声。由于键事件处理顺序是 KeyDown、KeyPress 和 KeyUp,因此 Enter 键仍然适用于控件的 KeyDown 事件。

于 2019-09-03T23:02:35.823 回答
2

我在尝试处理对我有用的 KeyDown 时偶然发现了这篇文章。

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

按下按键会停止将事件发送到底层控件。如果您手动处理输入键将在该文本框中执行的所有操作,这应该可以工作。对不起Visual Basic。

于 2013-04-26T14:43:02.007 回答
2

任何人都很少有机会得到这个答案,但其他一些答案真的很可怕。抑制事件KeyDown在一次打击中杀死 2 个额外事件。在这种情况下,将e.Handled属性设置为true是无用的。
最好的方法是将Form.AcceptButton属性设置为实际的搜索按钮。
还有另一种使用Enter键的方式——有些人可能希望它充当TAB按钮。为此,添加一个新的Button,将其Location属性设置在Form区域之外(即(-100, -100)) -在某些情况下,将Visible属性设置为false可能会禁用处理程序。ButtonForm.AcceptButton属性设置为新按钮。在Click事件处理程序中添加以下代码
this.SelectNextControl(ActiveControl, true, true, true, true)

现在,您可能只想在它打开focus时才进行传输,您可能想要测试类型或在不打算使用的控件的事件处理程序中使用属性就是 这样。你甚至不需要捕捉focusTextBoxActiveControle.SupressEnterTABe.KeyCode

于 2016-02-25T22:55:57.327 回答
1
$("#txtSomething").keypress(function (e) {
        if (e.which == 13) {

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

            //Write the rest of your code
        }
    });
于 2013-11-13T00:58:13.927 回答
0

将搜索按钮的IsDefault属性设置为true。这将使其成为默认按钮,并在按下 Enter 时自动单击。

于 2011-06-09T09:56:03.220 回答
0

好吧,我在这个问题上生活了足够长的时间,并在这里查找了它。

在考虑了很长时间并想要最简单的方法来修复它之后,我想出了最简单但不太优雅的修复方法。

这就是我所做的。

  1. 在表单上放置 2 个不可见的按钮“确定”和“取消”。
  2. 将窗体上的 AcceptButton 和 CancelButton 属性设置为不可见按钮。
  3. 没有向按钮添加代码!

这解决了该线程中列出的所有次要问题,包括 ToolStripMenu。我最大的抱怨是 BindingNavigator,当我将记录号输入到当前位置以导航到并按下回车时。

根据程序员在按下回车按钮时想要搜索功能的原始问题,我只是将搜索代码放在不可见的确定按钮中!

到目前为止,这似乎解决了所有问题,但我们都知道使用 Visual Studio,可能会出现一些问题。

我能想到的唯一另一种可能的优雅方法是编写一个新的击键处理类,这对我的大多数项目来说都是很多工作的方式。

于 2015-04-20T01:29:49.903 回答
0

您可以将文本框多行设置为 true,然后处理 Enter 按键。

private void yourForm_Load(object sender, EventArgs e)
    {
        textBox1.Multiline = true;
    }

//then write your TextBox codes
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        // doSomething();
    }
}
于 2020-02-22T09:43:44.450 回答
0

我更改了多行文本框的文本框属性,它对我有用。

于 2020-05-31T17:33:00.320 回答
0

关于e.SuppressKeyPress = true;解决方案,它本身就可以正常工作。设置SuppressKeyPress为true也设置Handled为true,所以不需要使用e.Handled= true;

于 2021-06-27T23:45:36.087 回答
-2
void RTextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyData == Keys.Enter)
    {
        //do ...
        bool temp = Multiline;
        Multiline = true;
        e.Handled = true;
        Multiline = temp;
    }
}
于 2013-03-02T15:44:50.447 回答