SelectItem of CTreeCtrl
-
Hi! how do i select 3th item of a CTreeCtrl? SelectItem() function needs HTREEITEM of specified item, but i have index of item (for example 3th item) please help me...
Zo.Naderi-Iran
It's not possible to access nodes by index. You must know the handle of the node. If you don't have records of the node handles, you can parse the tree:
// Start with the root item:
HTREEITEM hRoot = myTree.GetRootItem();// Get the first child of the root:
HTREEITEM hChild1 = myTree.GetChildItem(hRoot);// Move through the siblings until you reach the node you want:
HTREEITEM hChild2 = myTree.GetNextSiblingItem(hChild1)
HTREEITEM hChild3 = myTree.GetNextSiblingItem(hChild2) -
Hi! how do i select 3th item of a CTreeCtrl? SelectItem() function needs HTREEITEM of specified item, but i have index of item (for example 3th item) please help me...
Zo.Naderi-Iran
You could do something like this:
HTREEITEM ItemFromIndex(int index)
{
HTREEITEM item = m_tree.GetRootItem();
while (item != NULL && index > 0)
{
item = NextTreeItem(item);
--index;
}
// if item is not NULL, you now have the HTREEITEM of the given index
return item;
}HTREEITEM NextTreeItem(HTREEITEM hStartItem)
{
HTREEITEM hItem = hStartItem;
if (m_tree.ItemHasChildren(hItem))
{
// move down to next level
HTREEITEM hChildItem = m_tree.GetChildItem(hItem);
hItem = hChildItem;
}
else
{
bool foundSibling = false;
while (!foundSibling && hItem != NULL)
{
// move along same level
HTREEITEM hSiblingItem = m_tree.GetNextItem(hItem, TVGN_NEXT);
if (hSiblingItem != NULL)
{
hItem = hSiblingItem;
foundSibling = true;
}
else
{
// nothing on same level so move up and try again
hItem = m_tree.GetParentItem(hItem);
}
}
}
return hItem;
}Som eof this was written from memory, other sections are code pasts, so take with a pinch of salt.
If you vote me down, my score will only get lower