Solved

CTreeCtrl question

Posted on 1997-10-09
9
1,405 Views
Last Modified: 2013-11-20
I've got a CTreeCtrl with the TVS_EDITLABELS style in a modal dialog. When I'm editing a label, and presses TAB, I get the TVN_ENDLABELEDIT notification as expected.
I want the same thing to happen when I press return.
But if I press return when I'm editing a label, I only get a beep.
How can I make pressing the return key finish the label editing?
0
Comment
Question by:roar
  • 4
  • 4
9 Comments
 
LVL 1

Expert Comment

by:ohell
Comment Utility
Override the tree control's GetDlgCode() or so that it OR's the inherited classes' return value with DLGC_WANTMESSAGE,
Now, trap the control's OnKeyDown like this:
void CMyTreeCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
 CEdit *ctrl = GetEditControl();
 if (ctrl && nChar == VK_RETURN) {
  TV_DISPINFO info;
  info.hdr.hwndFrom = m_hWnd;
  info.hdr.idFrom = GetDlgCtrlId();
  info.hdr.code = TVN_ENDLABELEDIT;
  info.item = GetSelectedItem();
  m_parent->SendMessage(WM_NOTIFY, info.hdr.idFrom, (LPARAM)&info);
 }
}

This should do the trick, faking the ending of edit. Also, note that you need to include the ON_NOTIFY() and ON_GET_DLGCODE() macros in the message map for your control class.
0
 
LVL 1

Expert Comment

by:ohell
Comment Utility
Oh, also you need to add some lines in the if(ctrl && nChar == VK_RETURN) {} block above so that it'll look like

if (ctrl && nChar == VK_RETURN) {
  const HTREEITEM item = GetSelectedItem();
  if (ctrl->GetModify()) {
    CString txt = ctrl->GetWindowText();
    delete ctrl;
    SetItemText(item, (LPCTSTR)txt);
  }
  TV_DISPINFO info;
  info.hdr.hwndFrom = m_hWnd;
  info.hdr.idFrom = GetDlgCtrlId();
  info.hdr.code = TVN_ENDLABELEDIT;
  info.item.mask = TVIF_TEXT;
  info.item.hItem = item;
  VERIFY(GetItem(&info.item));
  m_parent->SendMessage(WM_NOTIFY, info.hdr.idFrom, (LPARAM)&info);  
}


0
 

Author Comment

by:roar
Comment Utility
I tried adding the code. When I'm not editing a label, the WM_KEYDOWN handler gets called. But after the label editing has started, it never gets called.
0
 

Author Comment

by:roar
Comment Utility
You said I should add ON_NOTIFY to the control class, could you explain that? I have added it to the dialog class as shown below, was that what you meant?

ON_NOTIFY(TVN_ENDLABELEDIT, IDC_MYTREE, OnEndLabelEditMyTree)
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:roar
Comment Utility
Hey, could you answer please??
0
 
LVL 1

Accepted Solution

by:
ohell earned 100 total points
Comment Utility
Yes, your notify syntax is correct, but it is very strange that you don't get the message in your control...I did something similar once, and it worked fine...are you sure you are not trapping key board messages elsewhere ? ALSO, re-reading your question, it seems very strange that you are getting a reverse behavior...usually tab gives a beep and return ends editing...

Another (rather untidy) way is to trap the keydown message in your dialog's PreTranslateMessage(), check if it is intended for this dialog and is a keydown message with the VK_ENTER key and an item is being edited, if it is, destroy the edit control after getting its text and setting it as the item text (you know the item number since you handled the LVN_BEGINLABELEDIT notification, which anyway you have to do to set the flag which lets you know that an item is being edited :)
This is just off the top of my head, and I haven't got the code for this, but it should work OK. I'll be glad to help though, if you keep on posting your code here.
Sorry for the delayed reply, but I was quite busy.
All the best
0
 

Author Comment

by:roar
Comment Utility
There is no other keyboard trapping messages. The dialog is very basic, just the CTreeCtrl and ok and cancel buttons. The ok button is initially disabled. If I enable it, the return key closes the dialog, also during label editing.
After the label editing has started, is it possible that the WM_KEYDOWN messages goes to the edit control instead of the CtreeCtrl? How can I trap WM_KEYDOWN in that case?
0
 
LVL 1

Expert Comment

by:ohell
Comment Utility
Yes, I think that edit control is getting your key down message, so I'd say you trap in message in the PreTranslateMessage of your dialog box...check the message to see if its a key pressed message and the key code is VK_ENTER...if so, then if a label is being edited, you can just get the text from the edit and put it in the tree and choose a return value such that the message is not translated further. (Documentation is available on CWnd::PreTranslateMessage)

The simplest way to check if the label is being edited is to handle TVN_BEGINLABELEDIT and set a flag, which would be unset when you get a VK_ENTER or VK_ESCAPE in PreTranslateMessage.

All the best
0
 

Expert Comment

by:RsTVS
Comment Utility
I have tried this code below in PreTranslateMsg(pMsg) . It worked fine for me
  if (pMsg->message == WM_KEYDOWN)
  {
    if (editable &&  pMsg->wParam == VK_RETURN)
    {
      ::TranslateMessage(pMsg);
      ::DispatchMessage(pMsg);
      return TRUE;
    }
  }
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now