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

后端 未结 9 2466
春和景丽
春和景丽 2020-12-15 02:38

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 d

9条回答
  •  孤城傲影
    2020-12-15 03:09

    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.

提交回复
热议问题