Solved

Wanted: A function to traverse a populated tree control

Posted on 2002-07-04
4
231 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

22 Experts available now in Live!

Get 1:1 Help Now