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

Solution 1:

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.

Solution 2:

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();
    }
}

Solution 3:

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

Solution 4:

Quick answer is that if you are using multiline true you have to explicitly call the select all.

private void tbUsername_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.A && e.Control)
    {
        tbUsername.SelectAll();
    }
}