Wanted: A function to traverse a populated tree control

Posted on 2002-07-04
Last Modified: 2013-11-20
Any of you folks has got such a function?

It is a CTreeCtrl that is a member of my own 'CMyTree' class.

It looks like a simple cookbook thing - traversing the tree, perhaps for printing out its contents.
Question by:yossikally
  • 2
LVL 12

Expert Comment

ID: 7131181

Author Comment

ID: 7131585
I know this article, and it's not what I meant.  It shows you how to extract the path to one specific node, and I want to traverse the whole tree.

Maybe the article can be adapted to do what I want, and eventually I may do it, but I wanted to save me some work.
LVL 12

Accepted Solution

migel earned 100 total points
ID: 7131610
it isn`t so difficult to adapt this code:

Traversing an Entire Tree View's Hierarchy

This article was contributed by Vinayak Tadas.

In a tree control it often becomes necessary to display the complete path in the tree from the selected item to the root. The path can be displayed as a string comprising of the labels of the tree items and starting from the root node to the selected item or from the selected item to the root item. The following function traverses the tree and retrieves the path from the selected item to the root.

//  GetTreeHeirarchy -Retrieves the tree heirarchy as a string comprising of item
labels from root node to selected node
//  hTItem -The HTREEITEM of selected item
//  cstrTreeHierarchy  -String to store the Tree heirarchy
//  bTopToBottom -TRUE  if the path should be from root to selected item
FALSE if the path should be from the selected item to the root

void CMyTreeCtrl::GetTreeHierarchy(HTREEITEM hTItem, CString &cstrTreeHierarchy)
HTREEITEM hChildItem = GetChildItem(hTItem);
if ( hChildItem == NULL ){
cstrTreeHierarchy += GetItemText(hTItem);
cstrTreeHierarchy += "\r\n";

while (hChildItem)
    cstrTreeHierarchy += "\\";
    GetTreeHierarchy(hChildItem, cstrTreeHierarchy);
    cstrTreeHierarchy += "\r\n";   // add new line
    hChildItem = GetNextItem(hChildItem, TVGN_NEXT);
CString csTreeHeirarchy;
m_pcMyTreeCtrl->GetTreeHierarchy(m_pcMyTreeCtrl->GetRootItem(), csTreeHeirarchy);

LVL 37

Expert Comment

ID: 7137001
Maybe this can help you:

HTREEITEM CTreeCtrlEx::GetNextItemEx(HTREEITEM hItemPara)
  HTREEITEM hItem = GetChildItem(hItemPara);
  if (hItem) return hItem;

  hItem = GetNextSiblingItem(hItemPara);
  if (hItem) return hItem;

  HTREEITEM hParent = hItemPara;
  while (!hItem)
    hParent = GetParentItem(hParent);
    if (!hParent) return NULL; // We are at the root (that has no parent)
    hItem = GetNextSiblingItem(hParent);
  return hItem;

And maybe you find these ones also interesting:

// Helpers to list out selected items. (Use similar to GetFirstVisibleItem(),
// GetNextVisibleItem() and GetPrevVisibleItem()!)

HTREEITEM CTreeCtrlEx::GetFirstSelectedItem()
     for ( HTREEITEM hItem = GetRootItem(); hItem!=NULL; hItem = GetNextVisibleItem( hItem ) )
          if ( GetItemState( hItem, TVIS_SELECTED ) & TVIS_SELECTED )
               return hItem;

     return NULL;

HTREEITEM CTreeCtrlEx::GetNextSelectedItem( HTREEITEM hItem )
     for ( hItem = GetNextVisibleItem( hItem ); hItem!=NULL; hItem = GetNextVisibleItem( hItem ) )
          if ( GetItemState( hItem, TVIS_SELECTED ) & TVIS_SELECTED )
               return hItem;

     return NULL;

HTREEITEM CTreeCtrlEx::GetPrevSelectedItem( HTREEITEM hItem )
     for ( hItem = GetPrevVisibleItem( hItem ); hItem!=NULL; hItem = GetPrevVisibleItem( hItem ) )
          if ( GetItemState( hItem, TVIS_SELECTED ) & TVIS_SELECTED )
               return hItem;

     return NULL;


Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Named range not carried over 10 63
java ^ examples 8 65
Use of condition with 'serial' in ansible 2 58
changeXy challenge 13 81
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now