按 Enter 时停止“叮" [英] Stop the 'Ding' when pressing Enter

查看:36
本文介绍了按 Enter 时停止“叮"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a very simple Windows Forms Application. And, in Windows (or, atleast Windows Forms Applications), when you press Enter while inside a Single-line TextBox Control, you hear a Ding. It's an unpleasent sound, that indicated you cannot enter a newline, because it is a single-line TextBox.

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

This is all fine. However, in my Form, I have 1 TextBox, and a Search Button. And I am allowing the user to Perform a search by pressing Enter after they've finished typing, so they don't have to use the mouse to click the Search Button.

但是出现了这个叮咚声.很烦人.

But this Ding sound occurs. It's very annoying.

我们如何才能让声音在我的表单中完全不播放?

How can we make it so just that sound doesn't play at all in my Form?

@David H - 这是我检测回车按下的方式:

@David H - Here's how I'm detecting the enter pressing:

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

推荐答案

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

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

来自文档:

此属性使您可以指定发生的默认操作用户在您的应用.分配给的按钮此属性必须是当前的 IButtonControl形成或位于容器内当前表单.

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.

还有一个 CancelButton 用户按下退出键时的属性.

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

这篇关于按 Enter 时停止“叮"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆