C# 如何在 winform 中允许 ctrl+a 与 TextBox 一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16197915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I allow ctrl+a with TextBox in winform?
提问by Patrik Lippojoki
I'm asking the question already asked (and even answered) here: Why are some textboxes not accepting Control + A shortcut to select all by default
我在这里问已经问过(甚至回答过)的问题: 为什么有些文本框不接受 Control + A 快捷方式来默认选择全部
But that answer doesn't work for me. I have this code:
但那个答案对我不起作用。我有这个代码:
public class LoginForm : Form
{
private TextBox tbUsername;
public LoginForm()
{
tbUsername = new TextBox();
tbUsername.ShortcutsEnabled = true;
tbUsername.Multiline = false;
Controls.Add(tbUsername);
}
}
The textbox shows up, I can write on it, I can cut, copy and paste text on it without any problems. But when I try to press Ctrl+AI only hear a "bling" similar to the bling that you hear if you try to erase text from an empty textbox (try it with your browser's address bar).
文本框出现了,我可以在上面写字,我可以在上面剪切、复制和粘贴文本,没有任何问题。但是,当我尝试按Ctrl+ 时,A我只会听到类似于您尝试从空文本框中擦除文本时听到的 bling 的“bling”(尝试使用浏览器的地址栏)。
采纳答案by jltrem
Like other answers indicate, Application.EnableVisualStyles()should be called. Also the TextBox.ShortcutsEnabledshould be set to true. But if your TextBox.Multilineis enabled thenCtrl+Awill not work(see MSDN documentation). Using RichTextBoxinstead will get around the problem.
像其他答案所表明的那样,Application.EnableVisualStyles()应该被调用。也TextBox.ShortcutsEnabled应该设置为true. 但是如果您TextBox.Multiline启用了Ctrl+A将不起作用(请参阅 MSDN 文档)。使用RichTextBox反而会得到解决的问题。
回答by Charles380
You could always override the process command keys to get the desired result
您始终可以覆盖进程命令键以获得所需的结果
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)
&& tbUsername.Focused)
{
tbUsername.SelectAll();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
回答by Charles380
This happened to me once too, I'm assuming you removed the call for Application.EnableVisualStyles();from your program? Add it back to the Main()function and everything should work fine.
这也发生在我身上一次,我假设你Application.EnableVisualStyles();从你的程序中删除了调用?将它添加回Main()函数,一切都应该正常工作。
回答by Charles380
No need to handle WM_KEYDOWN! 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.
无需处理 WM_KEYDOWN!我知道这里的大多数示例(以及 CodeProject 和许多其他地方)都说有,但是它并不能消除每当出现未处理的 WM_CHAR 时产生的蜂鸣声。
Instead, try this:
相反,试试这个:
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(...)
记住使用 WPA=SetWindowLong(...) 将 EDIT 控件子类化到这个 Edit_Prc() ,其中 WPA 是 CallWindowProc(...) 的窗口过程地址
I figured this out by experiment, after finding that all the answers I found online insisted on handling WM_KEYDOWN, using GetKeyState(), and ended up with bigger code that failed to stop that annoying beep!
我通过实验发现了这一点,在发现我在网上找到的所有答案都坚持使用 GetKeyState() 处理 WM_KEYDOWN 之后,最终得到了无法阻止烦人的哔哔声的更大代码!
While this answer doesn't deal with dotnet, in cases like this it's usually better to cut to the chase and solve it rather than agonise over which version of a large code wrapper system may or may not do it for you, especially if you want to avoid the risk of fighting against inbuilt behaviour.
虽然这个答案不涉及 dotnet,但在这种情况下,通常最好切入正题并解决它,而不是为大型代码包装系统的哪个版本可能会或可能不会为您做这件事而苦恼,特别是如果您想要以避免对抗内在行为的风险。
回答by Amit
Textbox has a method SelectAll()and worked well for me. (.net 4.5)
文本框有一种方法SelectAll(),对我来说效果很好。(.net 4.5)
回答by Stack Man
Just create a keydown event for that TextBox in question and include this code:
只需为有问题的 TextBox 创建一个 keydown 事件并包含以下代码:
private void tbUsername_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
if (sender != null)
((TextBox)sender).SelectAll();
}
}
回答by Nick Roberts
Quick answer is that if you are using multiline true you have to explicitly call the select all.
快速回答是,如果您使用多行 true,则必须明确调用全选。
private void tbUsername_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A && e.Control)
{
tbUsername.SelectAll();
}
}
回答by Barry Guvenkaya
Throwing in my two cents. Calling this under keypress is just another option.
投入我的两分钱。在按键下调用它只是另一种选择。
private void TxtBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\x1')
{
TxtBox.SelectAll();
e.Handled = true;
}
}

