按 CTRL-A 停止响铃 (WinForms)

发布于 2024-07-06 23:19:40 字数 762 浏览 7 评论 0原文

当使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

迷爱 2024-07-13 23:19:40
    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.A)
        {
            this.textBox1.SelectAll();
            e.SuppressKeyPress = true;
        }
    }

希望这可以帮助

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

hope this helps

你如我软肋 2024-07-13 23:19:40

感谢 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);
}

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);
}
梦情居士 2024-07-13 23:19:40

@H7O 解决方案很好,但我对表单上的多个 TextBox 组件进行了一些改进。

private void textBox_KeyDown(object sender, KeyEventArgs e)
{
  if (e.Control && e.KeyCode == Keys.A)
  {
    ((TextBox)sender).SelectAll();
    e.SuppressKeyPress = true;
  }
}

@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;
  }
}
与风相奔跑 2024-07-13 23:19:40

这对我有用:

将表单上的 KeyPreview 设置为 True。

This worked for me:

Set the KeyPreview on the Form to True.

深白境迁sunset 2024-07-13 23:19:40

今天尝试过,如果您对该行为太恼火,另一个限制选项是禁用快捷方式。 即:

txtContent.ShortcutsEnabled = false

注意:这还将禁用其他默认快捷键,例如 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.:

txtContent.ShortcutsEnabled = false

Note: This will also disable other default shortcuts like Ctrl+C/Ctrl+V/etc.. You can manually implement them like Ctrl+A solution in other answers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文