[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

How to get selected item from CTreeCtrl ?

Posted on 2000-02-08
7
Medium Priority
?
1,481 Views
Last Modified: 2013-11-20
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?
0
Comment
Question by:s1a1m2er
  • 4
  • 2
7 Comments
 
LVL 4

Expert Comment

by:pagladasu
ID: 2503027
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
 

Author Comment

by:s1a1m2er
ID: 2506993
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
 
LVL 4

Expert Comment

by:pagladasu
ID: 2507279
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:s1a1m2er
ID: 2507817
No it still doesnt works. I am using VC6. Any other solution? Kindly suggest..
0
 

Author Comment

by:s1a1m2er
ID: 2510934
any solution?
0
 
LVL 8

Accepted Solution

by:
Vinayak Kumbar earned 80 total points
ID: 2511163
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
 

Author Comment

by:s1a1m2er
ID: 2517708
ThankYou,VinExpert.It's working.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seveā€¦
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

591 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