Solved

CTreeCtrl question

Posted on 1997-10-09
9
1,464 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 1

Expert Comment

by:ohell
ID: 1307802
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
ID: 1307803
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
ID: 1307804
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:roar
ID: 1307805
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
 

Author Comment

by:roar
ID: 1307806
Hey, could you answer please??
0
 
LVL 1

Accepted Solution

by:
ohell earned 100 total points
ID: 1307807
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
ID: 1307808
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
ID: 1307809
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
ID: 36136705
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 54
ODBC Connection Logging, ADO.NET 6 73
NotAlone Challenge 20 91
scoreUp challenge 14 72
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
In this post we will learn different types of Android Layout and some basics of an Android App.
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Suggested Courses

736 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