Why are some textboxes not accepting Control + A shortcut to select all by default

后端 未结 5 1191
野趣味
野趣味 2020-12-08 10:05

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

相关标签:
5条回答
  • 2020-12-08 10:11

    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?

    0 讨论(0)
  • 2020-12-08 10:14

    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.

    0 讨论(0)
  • 2020-12-08 10:22

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

    That can disable Ctrl+A

    0 讨论(0)
  • 2020-12-08 10:26

    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;
          }
      }
    
    0 讨论(0)
  • 2020-12-08 10:28

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

    0 讨论(0)
提交回复
热议问题