61

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

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+A I 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).

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Patrik Lippojoki
  • 1,603
  • 4
  • 19
  • 22
  • FWIW your code works for me. I can use `CTRL + A` and other shortcuts. I thought `shift + up arrow` was supposed to select one letter a time. `shift and end` selects all. – keyboardP Apr 24 '13 at 17:22
  • yes it works me too.. what version of .net you're using? – spajce Apr 24 '13 at 17:23
  • @spajce Not sure, where can I check it? – Patrik Lippojoki Apr 24 '13 at 17:24
  • you can check it from the `properties` of your project. – spajce Apr 24 '13 at 17:35
  • @spajce "Target framework: `.NET Framework 4.5`" so I guess it's fairly new... – Patrik Lippojoki Apr 24 '13 at 17:37
  • ok.. I can't figure it out if this is really a bug but I just tested with .net 4.5 and your code is actually works.. I can `Ctrl+A` and so on.. – spajce Apr 24 '13 at 17:45
  • Is it possible that the CTRL+A command is being handled at a higher level? For example, if you override ProcessCmdKey at the Form level then it's possible to mark the event as being handled before the TextBox gets a chance to process it. Do you have a leftover key event handler that might be eating this event? – RogerN Apr 24 '13 at 17:58
  • ""Target framework: .NET Framework 4.5" so I guess it's fairly new..." Ummm, yeah, you could say that - it's actually the latest version. – Tim Apr 24 '13 at 18:02
  • lol, if i add a textbox, by default ctrl+A works. – string.Empty Apr 25 '13 at 11:36
  • Well that's basically all the code I have, on top of my `main()` function... – Patrik Lippojoki Apr 25 '13 at 18:26

9 Answers9

76

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.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
jltrem
  • 12,124
  • 4
  • 40
  • 50
  • 26
    The part about MultiLine must be talking about default behavior. Putting `if (e.Control && e.KeyCode == Keys.A) Textbox.SelectAll();` in the KeyDown event handler works just fine for me. – Dan Bechard Sep 02 '15 at 16:52
  • 4
    @Dan correct, you've got to work around it like that if you are using MultiLine and still require a standard textbox. A weird design decision (read as: bug) by MS. – jltrem Sep 02 '15 at 16:55
  • 5
    Broken again in 4.7, apparently. – Atario Sep 20 '17 at 20:32
  • Both `TextBox` and `RichTextBox` are derived from `TextBoxBase` Both have the `ShortcutsEnabled` property which seems to have no effect, at least with respect to ctrl-a. `RichTextBox` has a property `RichTextShortcutsEnabled` but it is not accessible, but maybe somehow underneath that is what enables ctrl-a for `RichTextBox`. – Al Lelopath May 23 '18 at 14:56
43

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();
    }
}
The_Black_Smurf
  • 5,178
  • 14
  • 52
  • 78
Stack Man
  • 866
  • 11
  • 22
24

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);
}
The_Black_Smurf
  • 5,178
  • 14
  • 52
  • 78
Charles380
  • 1,269
  • 8
  • 19
  • 2
    This is the quick and dirty solution for all those where "Application.EnableVisualStyles();" is not applicable. Very useful for me. Thanks. – Lars Sep 11 '13 at 13:58
  • If you want this to work even if the TextBox isn't the control with focus, then remove the code "&& tbUsername.Focused" and add this statement following the .SelectAll() call: "tbUsername.Focus();". See here: http://stackoverflow.com/questions/18050714/how-can-i-select-all-the-text-within-a-windows-forms-textbox – RenniePet Jun 30 '16 at 05:27
6

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();
    }
}
Nick Roberts
  • 245
  • 4
  • 12
3

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.

  • Thanks this fixed it. Is there a way to do this for one form only, so I would do like `MyForm.EnableVisualStyles();`? – Patrik Lippojoki Apr 26 '13 at 12:52
  • 8
    I am not sure this is the correct answer, since my project has this line added in Main, and CTRL+A is not working. Wierd... – Junior Mayhé Feb 08 '14 at 18:39
  • Likewise - not working for me - Application.EnableVisualStyles() is present and Ctrl+A not working, Ctrl+C is.... weird. – Morvael Feb 17 '14 at 15:34
1

Textbox has a method SelectAll() and worked well for me. (.net 4.5)

Amit
  • 25,106
  • 25
  • 75
  • 116
0

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.

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(...)

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!

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.

0

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;
    }
}
Baz Guvenkaya
  • 1,482
  • 3
  • 17
  • 26
0

This is my code, it is working fine

private void mainSimPlus_KeyDown(object sender, KeyEventArgs e)
            {
                e.Handled = true;
                if (e.Control == true && e.KeyCode == Keys.A)
                {
                    if (SelectAllTextBox(txt1))
                        return;
                    if (SelectAllTextBox(txt2))
                        return;
                }
            }
            private bool SelectAllTextBox(TextBox txt)
            {
                if (txt.Focused)
                {
                    txt.SelectAll();
                    return true;
                }
                else
                    return false;
            }
Chung Nguyen
  • 1,011
  • 1
  • 8
  • 8