Handling ESC and Return Keys While Editing a Label

When I first implemented the tree view control and tried editing a label, the only way I could finish editing was to click elsewhere. MFC has been designed in such a way that the parent window gets a chance at the messages using the PreTranslateMessage() virtual function. If the parent window is a CDialog or a CFormView, the ESC and the return key are handled by the parent and does not reach the control. To allow our control to process some important messages we need to override the PreTranslateMessage() function. In our code below, we also also allow the control key combinations to go through to the control. This allows copy, cut and paste using the control key combinations.

BOOL CTreeCtrlX::PreTranslateMessage(MSG* pMsg)
{
   if( pMsg->message == WM_KEYDOWN )
   {
      // When an item is being edited make sure the edit control
      // receives certain important key strokes
      if( GetEditControl()
         (pMsg->wParam == VK_RETURN
         || pMsg->wParam == VK_DELETE
         || pMsg->wParam == VK_ESCAPE
         || GetKeyState( VK_CONTROL)
      )
   )
   {
      ::TranslateMessage(pMsg);
      ::DispatchMessage(pMsg);
      return TRUE;    // DO NOT process further
    }
}

return CTreeCtrl::PreTranslateMessage(pMsg);


Comments

  • THANKS

    Posted by sam1982129 on 07/26/2006 05:15am

    OK

    Reply
  • It works

    Posted by ambhagat on 04/13/2005 10:43am

    Thanks Zafir - you saved me loads of time

    Reply
  • It also works for CDialogBar

    Posted by Legacy on 01/30/2004 12:00am

    Originally posted by: HB

    I was having the same problem with my derived CTreeCtrl on a CDialogBar. I knew that I had to override pretranslate but I was doing this instead.

    {
    WPARAM wparam=IDC_TREE_MAIN;
    LPARAM lparam=(LPARAM)&nmhdr;
    nmhdr.idFrom=IDC_TREE_MAIN;
    nmhdr.hwndFrom=GetSafeHwnd();
    nmhdr.code=TVN_ENDLABELEDIT;
    SendMessage(WM_NOTIFY, wparam, lparam);
    return TRUE;
    }

    so it was generating the TVN_ENDLABELEDIT message, but it wasn't actually ending the edit.

    Thanks for the solution!

    PS: A bunch of these MFC controls need to be redone.

    Reply
  • This is cool!! and so simple

    Posted by Legacy on 02/14/2003 12:00am

    Originally posted by: tudul

    .

    Reply
  • An easier way...

    Posted by Legacy on 04/05/2000 12:00am

    Originally posted by: Byron Montgomerie

    If you are using a DialogBox, they you should know that you
    
    get a WM_COMMAND message with the value of IDOK when you
    press return, this is part of the special handling by
    Dialog Boxes. Combine with with TreeView_EndEditLabelNow()
    and you are done. No MFC required either. :)

    case WM_COMMAND:

    if (!HIWORD(wParam))
    {
    switch(LOWORD(wParam))
    {
    case ...
    ...
    case IDOK:
    TreeView_EndEditLabelNow(GetDlgItem(hwnd, IDC_MYTREE), false);
    break;
    }
    }
    break;


    Hope this helps somebody. :)

    Reply
  • Easier way to cancel label editing

    Posted by Legacy on 08/25/1999 12:00am

    Originally posted by: Lisa Goldsmith

    If the editing of the label has been canceled via <ESC> or <RET> the pszText member of the LVITEM structure passed to OnEndlabeledit as part of the NMTVDISPINFO strucutre will be zero.  So a test of pszText before proceeding with your handler code will determine if editing was canceled and whether to continue.
    
    

    void CTreeCtrlX::OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult)
    {
    LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
    // TODO: Add your control notification handler code here
    // Get new label text and item number from struct
    // passed to this handler
    LVITEM pItem = pDispInfo->item;
    if (pItem.pszText == 0) // editing was cancelled
    return;
    CString sNewLabel = pItem.pszText;

    (Your Code Here)
    }


    Reply
  • Override PreTranslateMessage() of dialog; don't use TVN_KEYDOWN

    Posted by Legacy on 07/05/1999 12:00am

    Originally posted by: Damian Biollo

    You can put the code in the dialog class or the tree control.


    ON_NOTIFY(TVN_KEYDOWN ...) will not work because when you edit labels, because an Edit control gets created on the fly ... so the tree control is not receiving the key down messages.

    But if you want to handle key presses in the tree (eg. the Delete key) when NOT editing a label use TVN_KEYDOWN.

    Reply
  • Is the code correct

    Posted by Legacy on 05/26/1999 12:00am

    Originally posted by: P�l K. T�nder

    I tried the above code, and it only worked if I overrode
    the PreTranslateMessage method of the dialog, not the
    control

    Reply
  • ON_NOTIFY(TVN_KEYDOWN vs PreTranslateMessage

    Posted by Legacy on 04/19/1999 12:00am

    Originally posted by: Alexander Pavlovic

    Hmm, I thought MFC CTreeCtrl provides ON_NOTIFY(TVN_KEYDOWN
    macro for setting handler that catches any keypress within
    tree control. Overrides of PreTranslateMessage for any particular reason?


    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • When individual departments procure cloud service for their own use, they usually don't consider the hazardous organization-wide implications. Read this paper to learn best practices for setting up an internal, IT-based cloud brokerage function that service the entire organization. Find out how this approach enables you to retain top-down visibility and control of network security and manage the impact of cloud traffic on your WAN.

  • U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there is simply not enough quality talent to go around. Tiempo Development is a nearshore software development company. Our headquarters are in AZ, but we are a pioneer and leader in outsourcing to Mexico, based on our three software development centers there. We have a proven process and we are experts at providing our customers with powerful solutions. We transform ideas into reality.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date