Microsoft Knowledge Base Article - 167960
Suggest?
If you have any suggestions or comments on this article, please enter (up to 255 words)
support Center
Visual C Visual C .NET Visual Studio .NET
Bug: ESC / Enter Keys Do Not Work When Editing CTREECTRL LABELS for
THIS ARTICLE WAS Previously Published Under Q167960
SYMPTOMSWhen you edit labels in a CTreeCtrl, you are allowed to press the ESC key to cancel the changes or press the ENTER key to accept the changes. However, when the CTreeCtrl is a child window of a dialog box (CDialog), a formview window (CFormView), or a property page (cpropertyPage), The Esc or Enter Keys do not function as expected.
CAUSEIsDialogMessage () function is called in the PreTranslateInput () function which in turn is called in the PreTranslateMessage () function of all CFormView or CDialog derived classes mentioned above. The ESC and ENTER keys are processed in IsDialogMessage () but are not passed on to The Edit Control Created by The Tree-View Control. Thus, The Keystrokes Have No Effect.
RESOLUTIONFor Visual C 4.xx and later, we can trap the ESC and ENTER keystroke messages in the PreTranslateMessage () function for all CFormView, CDialog (modal or modeless) and CPropertyPage (either in modal or modeless CPropertySheet) derived classes.
In The Code Below, M_TreeCtrl IS A Member Variable of Those Deternal Classes and IS of CTREECTRL DATA TYPE, AND CMYXX CAN BE Any CFormview, CDIALOG, or CPROPERTYPAGE DERIVED CLASS.
BOOL CMyXxx :: PreTranslateMessage (MSG * pMsg) {// If edit control is visible in tree view control, when you send a // WM_KEYDOWN message to the edit control it will dismiss the edit // control. When the ENTER key was sent to the edit control, the // parent window of the tree view control is responsible for updating // the item's label in TVN_ENDLABELEDIT notification code. if (pMsg-> message == WM_KEYDOWN && pMsg-> wParam == VK_RETURN || pMsg- > wparam == vk_escape) {CEDIT * Edit = m_treectrl.GetEditControl (); if (edit) {Edit-> SendMessage (WM_KeyDown, PMSG-> WPARAM, PMSG-> LPARAM); returnxx}} // cxxxx can be a CFormView, Cdialog, or CPropertyPage class return CXxxx :: PreTranslateMessage (pMsg);.} for Visual C 2.xx, since PreTranslateMessage () is not called for modal CDialog-derived classes, the sample code above does not apply to the modal Dialog and Property Page In A Modal CpropertySheet Derived Class. In this situation, Those Keystroke Messages Can Be Trapped in The Overridden Onok .) (For Enter key) and OnCancel () (for Esc key) functions in either CDialog or CPropertySheet derived-class The sample code works fine even in the absence of both the OK and Cancel buttons from the dialog resource template.NOTE: It IS TOO LATE TO THOSE Keystroke Messages In CPropertyPage's Onok () and oncancel () Functions. Therefore, we have to do it in cpropertySheet-derived class.
A new member function called IsTreeCtrlEditMessage () is added to the CDialog or CPropertySheet derived-class. This function sends a WM_KEYDOWN message to the tree-view's edit control when it is the window with focus. And it is being called in both overridden OnOK ( ) and OnCancel () functions. The CMyDxxx in the sample code below can either be a CDialog or a CPropertySheet derived-class. Note that the MODAL_PROPERTYSHEET constant is declared and used in IsTreeCtrlEditMessage () so the same code can be applied to both CDialog and CPropertySheet Derived-classes.
// Set MODAL_PROPERTYSHEET to 1 for modal CPropertySheet-derived class // and 0 for CDialog-derived class #define MODAL_PROPERTYSHEET 1 BOOL CMyDxxx :: IsTreeCtrlEditMessage (WPARAM KeyCode) {BOOL rvalue = FALSE;. // pWnd is a pointer to either an active CPropertyPage of the modal // CPropertySheet or a modal CDialog object CWnd * pWnd = this;. #if MODAL_PROPERTYSHEET pWnd = GetActivePage (); #endif // IDC_TREECTRL is the ID of the tree view control CTreeCtrl * treectrl = (CTreeCtrl *. ) pWnd-> GetDlgItem (IDC_TREECTRL);! if (treectrl) return rvalue; // If the edit control of the tree view control has the input focus, // sending a WM_KEYDOWN message to the edit control will dismiss the // edit control . When ENTER key was sent to the edit control, the // parentwindow of the tree view control is responsible for updating // the item's label in TVN_ENDLABELEDIT notification code CWnd * focus = GetFocus ();. CEdit * edit = treectrl-> GetEditControl (); if (CEDIT *) FOCUS == Edit) {Edit-> SendMessage (WM _KEYDOWN, KeyCode); rvalue = TRUE;} return rvalue;} void CMyDxxx :: OnOK () {// Do not dismiss the dialog object if ENTER key was sent to the tree // view's edit control You may call the CDialog:. : Onok () Function if // this is for cdialog. If (! IstreeCtrleditMessage (VK_RETURN)) enddialog (idok);} void cMydxxx :: oncancel () {// do not dismiss the dialog object if esc key was Sent to the THE tree // view's edit control You might call the CDialog :: Cancel () // function if this is for CDialog if EndDialog (IDCANCEL).. (IsTreeCtrlEditMessage (VK_ESCAPE)!);} STATUSMicrosoft has confirmed this to be a bug in the Microsoft Products listed at the beginning of this article.