在CTRL-A(WinForms)上停止响铃

Blu*_*ers 13 keyeventargs 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

它有效,但是尽管e.Handled = true,系统铃声每次都会响起CTRL- A按下.


谢谢回复.

Form上的KeyPreview设置为true - 但这不会阻止系统响铃发声 - 这是我试图解决的问题 - 烦人.

小智 21

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.A)
        {
            this.textBox1.SelectAll();
            e.SuppressKeyPress = true;
        }
    }
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助


Blu*_*ers 6

感谢MSDN论坛帖子 - 这个问题只发生在文本框处于多行模式并且你想实现Ctrl+ A全部选择时.

这是解决方案

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
{
  if (keyData == (Keys.A | Keys.Control)) {
    txtContent.SelectionStart = 0;
    txtContent.SelectionLength = txtContent.Text.Length;
    txtContent.Focus();
    return true;
  }
  return base.ProcessCmdKey(ref msg, keyData);
}
Run Code Online (Sandbox Code Playgroud)


its*_*att 1

这对我有用:

将表单上的 KeyPreview 设置为 True。