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)
      return TRUE;    // DO NOT process further

return CTreeCtrl::PreTranslateMessage(pMsg);



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


  • It works

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

    Thanks Zafir - you saved me loads of time

  • 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.

    LPARAM lparam=(LPARAM)&nmhdr
    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.

  • This is cool!! and so simple

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

    Originally posted by: tudul


  • 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))
    case ...
    case IDOK:
    TreeView_EndEditLabelNow(GetDlgItem(hwnd, IDC_MYTREE), false);

    Hope this helps somebody. :)

  • 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)
    // 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
    CString sNewLabel = pItem.pszText;

    (Your Code Here)

  • 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.

  • 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

  • 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?

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

Top White Papers and Webcasts

  • The open source cloud computing project OpenStack has come a long way since NASA and Rackspace launched it in 2010. Backed by leading technology infrastructure providers including Cisco, Dell, EMC, HP, IBM, Intel, and VMware, OpenStack underpins significant workloads at an increasingly diverse set of organizations, including BWM, CERN, Comcast, eBay, and Wal-Mart. For CIOs engaged in broader programs to win, serve, and retain customers -- and refocus business technology (BT) spend -- a planned and pragmatic …

  • Protect Your Business against Today's Rapidly Growing Threats Your business, employees, partners, and customers are depending more and more on network connectivity, and protecting data and infrastructure has moved to the top of the IT agenda. You can't arm yourself against today's rapidly increasing threats unless you understand how they work. Read this ebook to learn more about the top 10 DNS attacks that can target your external and internal DNS infrastructure, the impact they can have on the DNS server …

Most Popular Programming Stories

More for Developers

RSS Feeds

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