按 CTRL-A 停止响铃 (WinForms)
当使用 CTRL-A 在 Winforms 应用程序中选择文本时,如何阻止系统铃声响起?
问题就在这里。 创建一个 Winforms 项目。 在窗体上放置一个文本框,并在窗体上添加以下事件处理程序,以允许 CTRL-A 选择文本框中的所有文本(无论哪个控件具有重点)。
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A && e.Modifiers == Keys.Control)
{
System.Diagnostics.Debug.WriteLine("Control and A were pressed.");
txtContent.SelectionStart = 0;
txtContent.SelectionLength = txtContent.Text.Length;
txtContent.Focus();
e.Handled = true;
}
}
它可以工作,但尽管 e.Handled = true,每次按下 CTRL-A 时系统铃声都会响起。
谢谢回复。
表单上的 KeyPreview 设置为 true - 但这并不能阻止系统铃声响起 - 这是我试图解决的问题 - 烦人。
Any ideas how to stop the system bell from sounding when CTRL-A is used to select text in a Winforms application?
Here's the problem. Create a Winforms project. Place a text box on the form and add the following event handler on the form to allow CTRL-A to select all the text in the textbox (no matter which control has the focus).
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A && e.Modifiers == Keys.Control)
{
System.Diagnostics.Debug.WriteLine("Control and A were pressed.");
txtContent.SelectionStart = 0;
txtContent.SelectionLength = txtContent.Text.Length;
txtContent.Focus();
e.Handled = true;
}
}
It works, but despite e.Handled = true, the system bell will sound every time CTRL-A is pressed.
Thanks for the reply.
KeyPreview on the Form is set to true - but that doesn't stop the system bell from sounding - which is the problem I'm trying to solve - annoying.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
希望这可以帮助
hope this helps
感谢 MSDN 论坛帖子 - 仅当文本框处于多行模式并且您希望实现 Ctrl+A 来全选时,才会出现此问题。
这是解决方案
Thanks to an MSDN Forum post - this problem only occurs when textboxes are in multiline mode and you'd like to implement Ctrl+A for select all.
Here's the solution
@H7O 解决方案很好,但我对表单上的多个 TextBox 组件进行了一些改进。
@H7O solution is good, but I improved it a bit for multiply TextBox components on the form.
这对我有用:
将表单上的 KeyPreview 设置为 True。
This worked for me:
Set the KeyPreview on the Form to True.
今天尝试过,如果您对该行为太恼火,另一个限制选项是禁用快捷方式。 即:
注意:这还将禁用其他默认快捷键,例如 Ctrl+C/Ctrl+V/etc。 您可以像其他答案中的
Ctrl+A
解决方案一样手动实现它们。Tried today, if you are too annoyed with the behavior, another limiting option is to disable shortcuts. i.e.:
Note: This will also disable other default shortcuts like
Ctrl+C/Ctrl+V/etc.
. You can manually implement them likeCtrl+A
solution in other answers.