Why are some textboxes not accepting Control + A s

2019-01-17 05:41发布

问题:

I don't know if the question is foolish, but i do find few textboxes here and there in my own program that accepts Control+A shortcut to select the entire text "by default" with "no coding".

I dunno what additional informations I've to give here as I find absolutely no difference between those textboxes. Those 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.

Edit: 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.

回答1:

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

EDIT:
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.



回答2:

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


回答3:

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?



回答4:

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

That can disable Ctrl+A



回答5:

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