按Enter键时停止'Ding'

ben*_*ndr 111 c# audio system-sounds winforms

我有一个非常简单的Windows窗体应用程序.而且,在Windows(或至少Windows窗体应用程序)中,当您在单行TextBox控件中按Enter时,您会听到一个丁.这是一种不愉快的声音,表示你无法输入换行符,因为它是一个单行的TextBox.

这一切都很好.但是,在我的表单中,我有1个TextBox和一个搜索按钮.而且我允许用户按Enter键,他们已经完成录入后进行搜索,所以他们不具备使用鼠标点击搜索按钮.

但是这个丁声就出现了.这很烦人.

我们如何才能使声音在我的表格中根本不发挥?

@David H - 这是我如何检测输入按下:

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        // Perform search now.
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 180

这个对我有用:

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;

     }

 }
Run Code Online (Sandbox Code Playgroud)

SuppressKeyPress是真正的伎俩.我希望能帮助你.

  • 在我看来,这是唯一有效的答案.`e.Handled = true;`不够; 是'SuppressKeyPress`完成了这个伎俩. (28认同)
  • 无论Handled还是SuppressKeyPress,textBox1_KeyUp都会在这种情况下发挥作用 (11认同)
  • 这应该是公认的答案.当前接受的答案需要在表格上放置标准按钮,这是令人不愉快的. (7认同)
  • 使用``ToolStripTextBox.KeyDown``为我工作.没有其他解决方案.谢谢! (4认同)
  • 它对我的“textbox”和“KeyDown”事件不起作用 (2认同)

FIr*_*nda 52

尝试

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
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @mdm取决于UI设计.当焦点在文本框上时,OP可能只想要这个动作.这很常见. (3认同)
  • 如果用户按Tab键,焦点将不再位于TextBox上,下一个将成为Focused的Contorl是Button Control,当你按Enter键时它不会发出声音.:-) (2认同)

mdm*_*mdm 52

查看Form.AcceptButton属性.您可以使用它来指定表单的默认按钮,在这种情况下是按Enter键.

来自文档:

使用此属性可以指定当用户在应用程序中按Enter键时发生的默认操作.分配给此属性的按钮必须是位于当前表单上或位于当前表单上的容器内的IButtonControl.

当用户按下escape时,还有一个CancelButton属性.

  • 这不适用于ToolStripButtons,或者如果要使用ENTER键验证TextBox或ToolStripTextBox.更好的方法是使用ToolStripTextBox为我工作的Lucio Fonseca的答案. (5认同)

bra*_*ong 12

只需添加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.
    }
}
Run Code Online (Sandbox Code Playgroud)


Maw*_*rdy 11

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

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

并向'丁'说和平

  • 这对我很有用.e.Handled显然禁用了'Ding'.将"提交"按钮(在我的情况下)设置为默认值,对我来说不起作用,因为我想对表单上的其他文本框以不同的方式处理"输入"键.<br/> <br/>顺便说一下:对于这个项目,我使用的是VB.所以我没有转换e.KeyChar,而是转换它:如果e.KeyChar = ChrW(Keys.Enter Then .... (2认同)

小智 7

用于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

      }
   }
}
Run Code Online (Sandbox Code Playgroud)