CTreeCtrl question

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?
roarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ohellCommented:
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
ohellCommented:
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
roarAuthor Commented:
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
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

roarAuthor Commented:
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
roarAuthor Commented:
Hey, could you answer please??
0
ohellCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
roarAuthor Commented:
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
ohellCommented:
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
RsTVSCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.