我在程序中发现了一些文本框,它们接受 Control+A 快捷方式来“默认”选择整个文本,并且“无编码”。
我不知道我必须在这里提供哪些附加信息才能为所有这些文本框启用它,因为我发现这些文本框之间绝对没有区别。它们都是简单的拖放文本框。
注意:我不是在谈论这段代码:
if (e.Control && e.KeyCode == Keys.A)
{
textBox1.SelectAll();
}
我想要默认选择...或者是否可以更改文本框属性以便文本框接受所有默认窗口快捷方式?
其他所有内容(Control + Z
、Control + X
、Control + C
、Control + 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.
发布评论
评论(5)
您可能正在寻找 ShortcutsEnabled 财产。将其设置为
true
将允许您的文本框实现 Ctrl+A 快捷键(等等)。从文档中:但是,文档 指出:
您可能必须使用
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:However, the documentation states:
You will probably have to use another subclass of
TextBoxBase
, such as RichTextBox, for that to work.实际上 CTRL + A 不会起作用,除非你添加如下内容:
Indeed CTRL + A will not work unless you add something like this:
这个答案在类似的问题中对我有用(未标记为已接受)
原始帖子:如何在 winform 中的 TextBox 上允许 ctrl+a ?
This answer worked for me in a similar question (which isn't marked as accepted)
Original Post: How can I allow ctrl+a with TextBox in winform?
确保
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
这个问题想要的答案不能以代码避免的形式给出,因为其他方法核心的 Win32 API 不允许这样做。如果其他方法允许,它们只是为您编写代码。 :)
所以真正的问题是:最小、最简洁的方法是什么?这对我有用:
首先,不需要处理 WM_KEYDOWN!也无需测试 Ctrl 键是否已经按下。我知道这里的大多数示例(以及 CodeProject 和许多其他地方)都说存在,但它并不能解决每当出现未处理的 WM_CHAR 时产生的蜂鸣声。
相反,尝试处理 WM_CHAR 并在那里进行 Ctrl+A 选择:
记住使用 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:
Remember to subclass the EDIT control to this Edit_Prc() using WPA=SetWindowLong(...) where WPA is the window procedure address for CallWindowProc(...)