Stop the Bell on CTRL-A (WinForms)

后端 未结 4 1292
野性不改
野性不改 2021-01-17 10:59

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

相关标签:
4条回答
  • 2021-01-17 11:24
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.A)
            {
                this.textBox1.SelectAll();
                e.SuppressKeyPress = true;
            }
        }
    

    hope this helps

    0 讨论(0)
  • 2021-01-17 11:27

    This worked for me:

    Set the KeyPreview on the Form to True.

    Hope that helps.

    0 讨论(0)
  • 2021-01-17 11:33

    @H7O solution is good, but I improved it a bit for multiply TextBox components on the form.

    private void textBox_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.Control && e.KeyCode == Keys.A)
      {
        ((TextBox)sender).SelectAll();
        e.SuppressKeyPress = true;
      }
    }
    
    0 讨论(0)
  • 2021-01-17 11:41

    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

    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);
    }
    
    0 讨论(0)
提交回复
热议问题