Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTREEITEM -- how to get text from it

Status
Not open for further replies.

jdeejay

Programmer
Oct 5, 2001
44
US
How do you use an HTREEITEM with a tree control.

I am trying to learn to pull from a database to populate a tree...If I have an array of HTREEITEMS, how can I later get the text out of it. i.e. If I am adding to the tree, I don't want to add a root if it is already there, so I want to compare the CString (that is my current potential addee) against all previous roots that were stored in the HTREEITEM array. something like
if (strtmp == rootItem)
but of course that doesn't work. I have been looking thru MSDN, but can't seem to determine exactly what the HTREEITEM is. Can some give me a one minute insight?

Thanks


 
Every example I see of using the Tree control adds the items by hard coding the name in. Not very realistic. Maybe HTREEITEM is not even what I need.

How do you grab info from a DB and add items to a tree but know where to put them? Somebody please point me in the right direction.
 
HTREEITEM is just a handle of the tree item. with HTREEITEM you can traverse the whose tree and you can get the data(text etc) from this tree item.

If you need more specific information, pls ask.
 
jfhuang,
thanks for the response. I have been so completely frustrated today. I think I was making things WAY too complicated for what I am trying to accomplish. My question was in how to get that text. Do I just keep track of any handles that I want and pass those handles back to GetItemText() to use the text? If so, I think I may have got it now.

john
 
You may find it easier to use the GetItemData() / SetItemData() members of CTreeCtrl to store a unique ID in each node of the tree. You can then use it to make any future comparisons (this allows you to ignore the displayed text or even use duplicate text in multiple nodes). Here's a condensed example of how to retrieve and/or store to ItemData (not guaranteed to be bug free):

[ignore]
void CTreeView::OnItemexpandingRptTree(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;

// work with the root
TV_ITEM TreeRoot = pNMTreeView->itemNew;
CTreeCtrl* pTree = (CTreeCtrl*) GetDlgItem(IDC_MyTree);

// get ItemData
int iTreeNodeID = (int)pTree->GetItemData(TreeRoot.hItem);

// work with one of the root item's siblings
HTREEITEM hRootItem;
HTREEITEM hChildItem;

if ((hRootItem = pTree->GetRootItem()) != NULL) {
if ((hChildItem = pTree->GetNextSiblingItem(hRootItem)) != NULL)
{
// get ItemData
iTreeNodeID = (int)pTree->GetItemData(hChildItem);
}
}

*pResult = 0;
}
[/ignore]

To insert a node and set ItemData, do something like this:

[ignore]
// NOTE: fill the TvInsert structure before inserting!
HTREEITEM hTreeItem = pTree->InsertItem(&TvInsert);

// set ItemData
pTree->SetItemData(hTreeItem, iTreeNodeID);
[/ignore]
 
jdeejay,
You are right,

GetItemText() and GetItem() can be used to get the text.
And you can use GetRootItem(), GetNextSiblingItem(),GetPreviousSiblingItem(),GetParentItem(),GetChildItem() to traverse the tree and get whatever HTREEITEM you like.

 
You use GetItemData() and SetItemData() to access ItemData, which is an extra place where data can be stored in each node (i.e. each HTREEITEM), in addition to the node's text.

Hope this helps! :cool:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top