Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Retrieving CTreeCtrl Item HTREEITEMs  (not RootItem)

Posted on 1998-07-21
2
Medium Priority
?
1,703 Views
Last Modified: 2008-02-01
I am using a TreeCtrl from within a CTreeView derived class and have called GetTreeCtrl() method to retrieve a reference. All fine there.

I would like to get a handle to one of the TVITEMs using GetItem() and passing a TV_ITEM structure reference.

Using this method I can retrieve the item text using HTREEITEM and TVIF_TEXT|TVIF_HANDLE masks.

What I would like to do however, is fill the structure with a string (.pszText) and get the control to return the item reference.

I have tried:

TV_ITEM tvi;
tvi.pszText=(LPSTR)(LPCTSTR)strItem;   //previously filled item string
tvi.mask=TVIF_TEXT|TVIF_HANDLE;

pctl->GetItem(&tvi);

What happens, if you look at the debug window is the .state to value 8a8b (or something like that) reference changes, and so does the .pszText (this is filled with what looks like invalid memory).  I have assumed therefore that the call is trying to return the text value by using the HTREEITEM .hitem, which is not what I want.

How can I retrieve the HTREEITEM of the item i want?

REgards

Oscar
0
Comment
Question by:ossentoo
[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
2 Comments
 
LVL 1

Accepted Solution

by:
fkurucz earned 210 total points
ID: 1168188
The HTREEITEM datatype is just an handle to an item.
The GetItem() function receives the associated data to the hItem handle.

To find an item by name, the tree has to be traversed.
This can be done recursiveley, starting from the root.

The following code is untested, but should work.


HTREEITEM hti = RecFindTreeItem("ThisOneToSearch", &MyTree);
// hti is now an handle to the searched item.
if (hti)
{
   // get data from hti
   DWORD dwData = MyTree.GetItemData(hti);
   ...
}


HTREEITEM RecFindTreeItem(
   const char *szCompare,
   CTreeCtrl *pTree,
   HTREEITEM hti = TVI_ROOT)
{
   HTREEITEM htiFound, htiNext, htiSub;
   char *szBuffer = new char[MAX_LEN];

   // test this item
   pTree->GetItemText(hti, szBuffer, MAX_LEN);
   if (strcmp(szBuffer, szCompare) == 0)
      return hti;

   // test items children
   htiSub = pTree->GetChildItem(hti);
   while (htiSub)
   {
      htiFound = RecursiveFindTreeItem(szCompare, pTree, htiSub);
      if (htiFound)
         return htiFound;
      htiSub = pTree->GetNextSiblingItem(htiSub);
   }
   return 0;
}


that should work.
If you exchange TEXT-data via the GetItem() funtion you always have to set the stringsize limmit in the cchTextMax field.

Hope it works,
  feri
0
 

Author Comment

by:ossentoo
ID: 1168189
Adjusted points to 75
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

618 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