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

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

  • Agile development principles have gone from something used only by cutting-edge teams to a mainstream approach used by teams large and small. If you're not using agile methods already though, or if you've only been exposed to agile on small projects here and there, you may wonder if agile can ever work in your environment. Read this eBook to learn the fundamentals of agile and how to increase the productivity of your software teams while enabling them to produce higher-quality solutions that better fulfill …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds