在 CTRL-A (WinForms) 上停止响铃 [英] Stop the Bell on CTRL-A (WinForms)
问题描述
当 CTRL-A 用于在 Winforms 应用程序中选择文本时,如何阻止系统响铃?
Any ideas how to stop the system bell from sounding when CTRL-A is used to select text in a Winforms application?
问题来了.创建一个 Winforms 项目.在窗体上放置一个文本框并在窗体上添加以下事件处理程序以允许 CTRL-A 选择文本框中的所有文本(无论哪个控件具有重点).
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;
}
}
它有效,但尽管 e.Handled = true,每次按下 CTRL-A 时系统铃声都会响起.
It works, but despite e.Handled = true, the system bell will sound every time CTRL-A is pressed.
谢谢回复.
表单上的 KeyPreview 设置为 true - 但这并不能阻止系统响铃 - 这是我正在尝试解决的问题 - 烦人.
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.
推荐答案
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
this.textBox1.SelectAll();
e.SuppressKeyPress = true;
}
}
希望能帮到你
这篇关于在 CTRL-A (WinForms) 上停止响铃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!