How to get selected item from CTreeCtrl ?

I have a class(CMyTreeView) derived from CTreeView. I added the following message handler function (for NM_CLICK message) to it-

void CMyTreeView::OnSingleClick(NMHDR* pNMHDR,LRESULT* pRESULT)

I want to get the selected tree item text. How's that possible?
s1a1m2erAsked:
Who is Participating?
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.

pagladasuCommented:
Try this code:

NM_TREEVIEW* pNMTreeView=(NM_TREEVIEW *)pNMHDR;
CTreeCtrl tree=GetTreeCtrl();
HTREEITEM hSelected=pNMTreeView->itemNew.hItem;
if(hSelected!=NULL){
   char text[31]; //the array where the text will be retrieved into
   TV_ITEM item;
   item.mask=TVIF_HANDLE | TVIF_TEXT;
   item.hItem=hSelected;
   item.pszText=text;
   item.cchTextMax=30; //maximum no of text - 30 bcos the array size is 31
   VERIFY(tree.GetItem(&item));
}
*pResult=0;

Thats about all. text will now contain the desired text.

thanks,
pagladasu
0
s1a1m2erAuthor Commented:
I am getting some junk text value when I do the above.(in text variable)
  The same above code works when I use it for TVN_SELCHANGED windows message handler but not for NM_CLICK message handler.
What's the problem? any solution?
0
pagladasuCommented:
The above code, as you had stated does work with TVN_SELCHANGED.
Try changing the third line to itemOld as follows:

      HTREEITEM hSelected=pNMTreeView->itemOld.hItem;

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

s1a1m2erAuthor Commented:
No it still doesnt works. I am using VC6. Any other solution? Kindly suggest..
0
s1a1m2erAuthor Commented:
any solution?
0
Vinayak KumbarSr Program ManagerCommented:
Hi,

If U r going for click, then I will suggest to go for LButtonDown, so map the WM_LBUTTONDOWN message to Ur TreeCtrl and modify as follows
void CMyTree::OnLButtonDown(UINT nFlags, CPoint point)
{
      UINT uint;
      HTREEITEM selitem = HitTest(point, &uint);

      if(selitem)
      {
            TV_ITEM item;
            item.mask=TVIF_HANDLE | TVIF_TEXT;
            item.hItem=selitem;
            item.pszText=text;
            item.cchTextMax=30;
            GetItem(&item);//Now Ur item will contain every info about the clicked event
      }
      
      CTreeCtrl::OnLButtonDown(nFlags, point);
}

OnLButtonDown will be called before the OnClick function. It is not working because OnClick will be called before the OnSelChange, so selection is not done yet. So I am going for LButtonDown.

Try this out.
VinExpert
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
s1a1m2erAuthor Commented:
ThankYou,VinExpert.It's working.
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.