CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2003
    Location
    Korat, Thailand
    Posts
    112

    Why WM_CHAR message is ignored in CFormview views?

    Hi all

    I built a CFormview view containing many edit boxes and I wanted the user is enable to move the edit boxes' focus from one the another by using arrow key insteed of Tab key. But I's just frustrated. Does anyone know why WM_CHAR message is ignored in CFormview views?

    Thank you in advance
    Monch

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    Post the code where you catch WM_CHAR message in the formview.

    Kuphryn

  3. #3
    Join Date
    Sep 1999
    Location
    Colorado, USA
    Posts
    1,002
    Regardless of how you implement it, you cannot catch an arrow keys with a WM_CHAR message. Instead, catch WM_KEYDOWN messages for arrow keys.
    You can PreTranslateMessage in the FormView or have all your edit boxes be a derived form of CEdit which catches WM_KEYDOWN messages and if it is an arrow key, sends a user defined message to the FormView parent with the ID of the edit box and the key code. If you make all the edit boxes with contiguous IDs, it is then an easy matter to switch focus to the appropriate edit control.
    Steve

  4. #4
    Join Date
    Sep 2003
    Location
    Korat, Thailand
    Posts
    112
    Thanks Steve alot. I will try it.


    Reply to Kuphryn

    // in .h file
    //{{AFX_MSG(CGetPay)
    ...
    afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
    ...
    //}}AFX_MSG


    // in .cpp file
    void CGetPay::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    MessageBox("");
    CFormView::OnChar(nChar, nRepCnt, nFlags);
    }

    CGetPay is derived from CFromView. No matter key I stroke down when the view is active, no messege box appears as coding.

  5. #5
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    Actually, to get the arrow keys you need to process the WM_KEYDOWN message and the WM_GETDLGCODE message. You should be aware that if an edit box has keyboard focus, the any keypresses will be process by that edit box. Therefore you will need to derive your own CEdit class for your edit boxes and process the WM_KEYDOWN messages and WM_GETDLGCODE message in the derived class. The derived class will need to use a user defined message (which it sends back to the form view) to implement the behavior you want. By implementing behavior other than the default keypress behavior you lose the ability to move the cursor in the edit box. This will be unexpected to the user and will make editing typing mistakes more difficult. I would highly recommend staying with the default TAB/SHIFT+TAB behavior. Doing it with the arrow keys will result in more code and less performance. That is never a good thing.

    However, if you really want to do it with arrow keys here is the code you would need in the derived CEdit class.

    Code:
    void CYourClass::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
    {
       if(nChar==VK_DOWN)
       {
          ....Do something for the down arrow....
          return;
       }
       if(nChar==VK_UP)
       {
          ....Do something for the up key....
          return;
       }
       if(nChar==VK_LEFT)
       {
          ....Do something for the left key....
          return;
       }
       if(nChar==VK_RIGHT)
       {
          ....Do something for the right key....
          return;
       }    
       else	
          CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
    }
    
    UINT CYourClassOnGetDlgCode() 
    {
         return DLGC_WANTARROWS;
        //return DLGC_WANTALLKEYS;
    }

    T
    Last edited by TDM; April 25th, 2004 at 11:26 PM.

  6. #6
    Join Date
    Sep 2003
    Location
    Korat, Thailand
    Posts
    112
    Oh I see.
    Thanks TDM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured