Solved

Wanted: A function to traverse a populated tree control

Posted on 2002-07-04
4
269 Views
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.
0
Comment
Question by:yossikally
[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
4 Comments
 
LVL 12

Expert Comment

by:migel
ID: 7131181
0
 

Author Comment

by:yossikally
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.
0
 
LVL 12

Accepted Solution

by:
migel earned 100 total points
ID: 7131610
ok
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";
return;
}

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);


0
 
LVL 37

Expert Comment

by:zzynx
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;
}

Success.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 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