Re: Traversing shallow CTreeCtrl



I get a little scared when I see three variables here. Why not write a recursive function
that takes exactly on HTREEITEM

void CMyClass::Traverse(HTREEITEM item)
{
if(MyTree.ItemHasChildren(item))
{ /* iterate over children */
HTREEITEM subitem = MyTree.GetNextItem(item, TVGN_CHILD);
while(subitem != NULL)
{ /* recur */
Traverse(subitem);
subitem = MyTree.GetNextItem(subitem, TVGN_NEXT);
} /* recur */
} /* iterate over children */
}

invoke your traversal by doing:
Traverse(MyTree.GetRootItem());

To accumulate the information, you might pass in a refernce to a CArray or other suitable
data structure that you want to fill with the information you discover. Using a fixed
number of levels usually ends up being more difficult than a simple recursion.
joe


On 9 May 2006 02:50:01 -0700, msandler@xxxxxxxxxxxxxxxxxxx wrote:

I have succesfully loaded my CTreeCtrl in my dialog with hierarchical
data. I want the user to check off the boxes next to the data he wants,
and then when he clicks OK to compile a CList of that data to be acted
apon.

The data is only three levels deep (excluding the root), and only the
2nd and 3rd levels of data are significant when checked.

I am having trouble traversing the tree. As near as I can see
GetNextItem is not returning the next child. I haven't used a recursive
function because the tree is so shallow,and I need to know what level
of data I'm for compiling the CList.

HTREEITEM Root, LvlOneNode, LvlTwoNode, LvlThreeNode;
Root = MyTree.GetRootItem();
LvlOneNode = MyTree.GetNextItem(NULL, TVGN_CHILD);
while (LvlOneNode != NULL)
{
LvlTwoNode = MyTree.GetNextItem(NULL, TVGN_CHILD);
while (LvlTwoNode != NULL)
{
if (MyTree.ItemHasChildren(LvlTwoNode))
{
LvlThreeNode = MyTree.GetNextItem(NULL, TVGN_CHILD);
while (LvlThreeNode != NULL)
{
if (MyTree.GetCheck(LvlThreeNode))
{

SelectedLvlThree.AddHead((LvlThreeData*)(MyTree.GetItemData(LvlThreeNode)));
}
LvlThreeNode = MyTree.GetNextItem(LvlThreeNode, TVGN_NEXT);
}
}
else
{
if (MyTree.GetCheck(LvlTwoNode))
{

SelectedLvlTwo.AddHead((LvlTwoData*)(MyTree.GetItemData(LvlTwoNode)));
}
}
LvlTwoNode = MyTree.GetNextItem(LvlTwoNode, TVGN_NEXT);
}
LvlOneNode = MyTree.GetNextItem(LvlOneNode, TVGN_NEXT);
}

Thanks for any advice.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
--
NewsGuy.Com 30Gb $9.95 Carry Forward and On Demand Bandwidth
.



Relevant Pages

  • Debug my program please.
    ... I have an error in to recursive function aggiungiat first recursion. ... !character,allocatable:: Parola ... Reinizializzazione della parola if (.not. ...
    (comp.lang.fortran)
  • Re: Timing Rescordset
    ... The recursive function I am using first check to see if it has something, ... avoid levels of recursion that lack substantial advantage. ... XML authors use the same node name in a different heirarchy? ... To reconstruct, you'll have to query the db for each level, in much the same ...
    (microsoft.public.data.ado)
  • Re: Any use for recursion?
    ... popped back off the stack, the program counter being changed again. ... Also, if the recursion is deep, e.g. 16 levels deep, then it results ... can be done with a plain loop can be done with a recursive function. ...
    (comp.programming)
  • Re: Iteration in lisp
    ... recursive function can run out of stack since CL does not guarantee ... archives of this newsgroup for the TAILPROG macro, ... tail recursion with syntax resembling FLET or LABELS can be obtained ...
    (comp.lang.lisp)
  • Re: error handlling in recursive function
    ... has failed and you want to set the error flag and return it to the ... calling function(the one which called the recursive function in the ... Use setjmp (before entering recursion) and longjmp to hop back on ... Ok, global variables are EVIL, ...
    (comp.lang.c)