为什么有些文本框不接受 Control +默认选择全部的快捷方式

发布于 2024-11-05 08:30:21 字数 754 浏览 1 评论 0 原文

我在程序中发现了一些文本框,它们接受 Control+A 快捷方式来“默认”选择整个文本,并且“无编码”。

我不知道我必须在这里提供哪些附加信息才能为所有这些文本框启用它,因为我发现这些文本框之间绝对没有区别。它们都是简单的拖放文本框。

注意:我不是在谈论这段代码:

if (e.Control && e.KeyCode == Keys.A)
{
    textBox1.SelectAll();
}

我想要默认选择...或者是否可以更改文本框属性以便文本框接受所有默认窗口快捷方式?

其他所有内容(Control + ZControl + XControl + CControl + V)默认有效!为什么不Control + A

更新:默认接受Ctrl+A的文本框是屏蔽文本框,而不是常规文本框。那时我正在使用 .NET 2.0。但我猜最初的问题是别的问题,因为我可以看到 Ctrl+A 在 .NET 2.0 代码中默认工作正常。

I have found a few textboxes here and there in my program that accepts Control+A shortcut to select the entire text "by default" with "no coding".

I don't know what additional information I have to give here to enable it for all of them, as I find absolutely no difference between these textboxes. They are all simple dragged and dropped textboxes.

Note: I'm not talking about this piece of code:

if (e.Control && e.KeyCode == Keys.A)
{
    textBox1.SelectAll();
}

I want selection by default... or is there anyway to change textbox property so that textboxes accept all default windows shortcuts?

Everything else (Control + Z, Control + X, Control + C, Control + V) works by default! Why not Control + A?

Update: The text boxes that accepted Ctrl+A by default were masked textboxes, not the regular one. And at that point I was with .NET 2.0. But I guess the original problem was something else, as I can see Ctrl+A working fine by default in .NET 2.0 code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

辞别 2024-11-12 08:30:21

您可能正在寻找 ShortcutsEnabled 财产。将其设置为 true 将允许您的文本框实现 Ctrl+A 快捷键(等等)。从文档中:

使用 ShortcutsEnabled 属性
启用或禁用以下功能
快捷键组合:

  • CTRL+Z

  • CTRL+E

  • CTRL+C

  • CTRL+Y

  • CTRL+X

  • CTRL+退格键

  • CTRL+V

  • CTRL+删除

  • CTRL+A

  • SHIFT+删除

  • CTRL+L

  • SHIFT+插入

  • CTRL+R

但是,文档 指出:

TextBox 控件不当 CTRL+A 快捷键“nofollow noreferrer">Multiline 属性值为 true。

您可能必须使用 TextBoxBase 的另一个子类,例如 RichTextBox,才能正常工作。

You might be looking for the ShortcutsEnabled property. Setting it to true would allow your text boxes to implement the Ctrl+A shortcut (among others). From the documentation:

Use the ShortcutsEnabled property to
enable or disable the following
shortcut key combinations:

  • CTRL+Z

  • CTRL+E

  • CTRL+C

  • CTRL+Y

  • CTRL+X

  • CTRL+BACKSPACE

  • CTRL+V

  • CTRL+DELETE

  • CTRL+A

  • SHIFT+DELETE

  • CTRL+L

  • SHIFT+INSERT

  • CTRL+R

However, the documentation states:

The TextBox control does not support the CTRL+A shortcut key when the Multiline property value is true.

You will probably have to use another subclass of TextBoxBase, such as RichTextBox, for that to work.

傲鸠 2024-11-12 08:30:21

实际上 CTRL + A 不会起作用,除非你添加如下内容:

  private void textBox1_KeyDown(object sender, KeyEventArgs e)
  {
      if (e.Control && (e.KeyCode == Keys.A))
      {
          if (sender != null)
               ((TextBox)sender).SelectAll();
          e.Handled = true;
      }
  }

Indeed CTRL + A will not work unless you add something like this:

  private void textBox1_KeyDown(object sender, KeyEventArgs e)
  {
      if (e.Control && (e.KeyCode == Keys.A))
      {
          if (sender != null)
               ((TextBox)sender).SelectAll();
          e.Handled = true;
      }
  }
影子是时光的心 2024-11-12 08:30:21

这个答案在类似的问题中对我有用(未标记为已接受)

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    const int WM_KEYDOWN = 0x100;
    var keyCode = (Keys) (msg.WParam.ToInt32() &
                          Convert.ToInt32(Keys.KeyCode));
    if ((msg.Msg == WM_KEYDOWN && keyCode == Keys.A) 
        && (ModifierKeys == Keys.Control) 
        && txtYourTextBox.Focused)
    {
        txtYourTextBox.SelectAll();
        return true;
    }            
    return base.ProcessCmdKey(ref msg, keyData);
}

原始帖子:如何在 winform 中的 TextBox 上允许 ctrl+a ?

This answer worked for me in a similar question (which isn't marked as accepted)

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    const int WM_KEYDOWN = 0x100;
    var keyCode = (Keys) (msg.WParam.ToInt32() &
                          Convert.ToInt32(Keys.KeyCode));
    if ((msg.Msg == WM_KEYDOWN && keyCode == Keys.A) 
        && (ModifierKeys == Keys.Control) 
        && txtYourTextBox.Focused)
    {
        txtYourTextBox.SelectAll();
        return true;
    }            
    return base.ProcessCmdKey(ref msg, keyData);
}

Original Post: How can I allow ctrl+a with TextBox in winform?

岁月蹉跎了容颜 2024-11-12 08:30:21

确保
Application.EnableVisualStyles();
没有被注释掉
static void Main()

可以禁用 Ctrl+A

Make sure that
Application.EnableVisualStyles();
is not commented out in
static void Main()

That can disable Ctrl+A

像你 2024-11-12 08:30:21

这个问题想要的答案不能以代码避免的形式给出,因为其他方法核心的 Win32 API 不允许这样做。如果其他方法允许,它们只是为您编写代码。 :)

所以真正的问题是:最小、最简洁的方法是什么?这对我有用:

首先,不需要处理 WM_KEYDOWN!也无需测试 Ctrl 键是否已经按下。我知道这里的大多数示例(以及 CodeProject 和许多其他地方)都说存在,但它并不能解决每当出现未处理的 WM_CHAR 时产生的蜂鸣声。

相反,尝试处理 WM_CHAR 并在那里进行 Ctrl+A 选择:

LRESULT CALLBACK Edit_Prc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
  if(msg==WM_CHAR&&wParam==1){SendMessage(hwnd,EM_SETSEL,0,-1); return 1;}
  else return CallWindowProc((void*)WPA,hwnd,msg,wParam,lParam);
}

记住使用 WPA=SetWindowLong(...) 将 EDIT 控件子类到此 Edit_Prc(),其中 WPA 是 CallWindowProc(...) 的窗口过程地址

This question wants an answer that cannot be given in the form of code avoidance, as the Win32 API at the core of the other methods doesn't allow it. If other methods DO allow it, they are just writing the code for you. :)

So the real question is: What is the smallest, neatest way to do it? This worked for me:

First, there is no need to handle WM_KEYDOWN! And no need to test for the Ctrl key already down either. I know that most examples here (and CodeProject and many other places) all say there is, but it does not cure the beep that results whenever a WM_CHAR arises that is not handled.

Instead, try handling WM_CHAR and doing the Ctrl+A selection there:

LRESULT CALLBACK Edit_Prc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
  if(msg==WM_CHAR&&wParam==1){SendMessage(hwnd,EM_SETSEL,0,-1); return 1;}
  else return CallWindowProc((void*)WPA,hwnd,msg,wParam,lParam);
}

Remember to subclass the EDIT control to this Edit_Prc() using WPA=SetWindowLong(...) where WPA is the window procedure address for CallWindowProc(...)

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