Windows“叮”声在文本框中使用 ctrl+A 时播放声音 (C#)
通过在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至少在带有Windows Forms的Windows XP SP3上,同样的情况发生在我身上(这真的很烦人)。
即使没有任何事件处理程序,也会发出“叮”声。多行和其他设置(预览、输入键等)也不起作用。
我使用这个事件处理程序来摆脱它:
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:
叮当声表明发生了错误,所以我猜测这行代码是导致问题的原因:因为计数为 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:Because it's a 0 count, you really want to scroll totextBox1.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.