43
votes

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
My experience is that no text-boxes respond to Ctrl+A by default; I have had to implement it myself (as per your code).Polyfun
@Heandel, sorry for belated reply, I was out of station unexpectedly. I think there's no need of examples as mostly textboxes doesnt accept Control + A by default. Actually very few does without coding even. May be for that I need to deliver examples. Which I dunno how to do.nawfal
@ShellShock, yes thats right. But some textboxes in my application does!! How on earth is that..nawfal
Can anybody provide me a neat shortcut with which I can just tweak the textbox property in GUI section so that it accepts all default Windows shortcuts.nawfal
It's fixed .NET 4.6.1, credits go to: link It works with ReadOnly and MultiLine = trueArieKanarie

5 Answers

66
votes

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:

Use the ShortcutsEnabled property to enable or disable the following shortcut key combinations:

  • CTRL+Z

  • CTRL+E

  • CTRL+C

  • CTRL+Y

  • CTRL+X

  • CTRL+BACKSPACE

  • CTRL+V

  • CTRL+DELETE

  • CTRL+A

  • SHIFT+DELETE

  • CTRL+L

  • SHIFT+INSERT

  • CTRL+R

However, the documentation states:

The TextBox control does not support the CTRL+A shortcut key when the Multiline property value is true.

You will probably have to use another subclass of TextBoxBase, such as RichTextBox, for that to work.

26
votes

Indeed CTRL + A will not work unless you add something like this:

  private void textBox1_KeyDown(object sender, KeyEventArgs e)
  {
      if (e.Control && (e.KeyCode == Keys.A))
      {
          if (sender != null)
               ((TextBox)sender).SelectAll();
          e.Handled = true;
      }
  }
4
votes

This answer worked for me in a similar question (which isn't marked as accepted)

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) 
        && txtYourTextBox.Focused)
    {
        txtYourTextBox.SelectAll();
        return true;
    }            
    return base.ProcessCmdKey(ref msg, keyData);
}

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

2
votes

Make sure that Application.EnableVisualStyles(); is not commented out in static void Main()

That can disable Ctrl+A

1
votes

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:

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