Solved

Wanted: A function to traverse a populated tree control

Posted on 2002-07-04
4
220 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
Comment Utility
0
 

Author Comment

by:yossikally
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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 …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

772 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