Windows“叮”声在文本框中使用 ctrl+A 时播放声音 (C#)

发布于 2024-10-21 05:04:17 字数 270 浏览 4 评论 0原文

通过在 Control 设置为 True 时监听文本框的 KeyDown 事件以查找“A”按键,创建支持“ctrl+A”(全选)的文本框非常容易。当满足此条件时,文本框会执行如下调用:

textBox1.Select(0, textBox1.Text.Length);
textBox1.ScrollToCaret();

“全选”功能运行良好,只是当我在文本框中实际键入 ctrl+A 时,我会听到 Windows“叮”声。使用该应用程序。我不明白为什么。

It was easy enough to create a text box that supports "ctrl+A" (select all), by listening in on the text box's KeyDown event for an "A" keypress when Control is set to True. When this condition is met, the text box does a call like this:

textBox1.Select(0, textBox1.Text.Length);
textBox1.ScrollToCaret();

The "select all" functionality works well enough, except that I hear the windows "ding" sound when I actually type ctrl+A into my text box when I'm using the application. I can't figure out why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍景流年 2024-10-28 05:04:17

至少在带有Windows FormsWindows XP SP3上,同样的情况发生在我身上(这真的很烦人)。

即使没有任何事件处理程序,也会发出“叮”声。多行和其他设置(预览、输入键等)也不起作用。

我使用这个事件处理程序来摆脱它:

public static void TextBoxSelectAll(object sender, KeyEventArgs e)
{
    if (e.KeyData == (Keys.Control | Keys.A))
    {
        ((TextBox)sender).SelectAll();

        e.SuppressKeyPress = true;
        e.Handled = true;
    }
}

At least on Windows XP SP3 with Windows Forms, the same happens to me (it is really annoying).

The "ding" sound is played even without any event handlers. Multiline and other settings (preview, input keys, etc.) also have no effect.

I use this event handler to get rid of it:

public static void TextBoxSelectAll(object sender, KeyEventArgs e)
{
    if (e.KeyData == (Keys.Control | Keys.A))
    {
        ((TextBox)sender).SelectAll();

        e.SuppressKeyPress = true;
        e.Handled = true;
    }
}
深空失忆 2024-10-28 05:04:17

叮当声表明发生了错误,所以我猜测这行代码是导致问题的原因:

textBox1.Select(0, textBox1.Text.Length);

因为计数为 0,所以您确实想要滚动到 textBox1 .Text.Length - 1。 (不过,我有点猜测。而且,正如丹尼尔所说,这个功能已经内置了......不需要实现它!)

编辑 - 问题(如所描述的<当文本框处于多行模式时,会出现 href="https://stackoverflow.com/questions/225711/stop-the-bell-on-ctrl-a-winforms/230417#230417">此处)。点击链接修复该问题。

The ding sound indicates an error has occurred, so my guess is that this line of code is what's causing the problem:

textBox1.Select(0, textBox1.Text.Length);

Because it's a 0 count, you really want to scroll to textBox1.Text.Length - 1. (I am guessing a bit, however. And, as Daniel said, this functionality is already built in...no need to implement it!)

Edit - The problem (as described here) occurs when textboxes are in multiline mode. Follow the link to a fix for the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文