Solved

Wanted: A function to traverse a populated tree control

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iSeries FTP Exit Program 8 147
How do you programatically show and hide the Windows 10 On-Screen-Keyboard? 3 805
maxBlock challenge 30 142
Annoying "thing" blocks my view 4 117
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: 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…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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