How can I allow ctrl+a with TextBox in winform?
Like other answers indicate, Application.EnableVisualStyles()
should be called. Also the TextBox.ShortcutsEnabled
should be set to true
. But if your TextBox.Multiline
is enabled then Ctrl+A will not work (see MSDN documentation). Using RichTextBox
instead will get around the problem.
Just create a keydown event for that TextBox in question and include this code:
private void tbUsername_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
if (sender != null)
((TextBox)sender).SelectAll();
}
}
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);
}