Iterating Over All Tree Items in wxTreeCtrl

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Everydaydiesel
Earned some good credits
Earned some good credits
Posts: 125
Joined: Wed Oct 28, 2015 9:48 pm

Iterating Over All Tree Items in wxTreeCtrl

Post by Everydaydiesel »

Hello, I have the following recursive code but the problem is that it does not iterate over the last tree view item. I am unsure why really.

Code: Select all

void Form::GetAllTreeViewItems(wxTreeCtrl *tv, const wxTreeItemId& tiiCurrent)
{
    wxTreeItemIdValue cookie;
    wxTreeItemId child = tv->GetFirstChild(tiiCurrent, cookie);

    while (child.IsOk())
    {
        m_tiiAllTreeViewItems.push_back(child);
        string sCurrentPath = GetPathOfTreeViewItem(child);
        wxMessageBox(sCurrentPath);

        GetAllTreeViewItems(tv, child);
        child = tv->GetNextChild(child, cookie);
    }
    
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Iterating Over All Tree Items in wxTreeCtrl

Post by ONEEYEMAN »

Hi,
So it prints all of them in the message box but the last one?

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Iterating Over All Tree Items in wxTreeCtrl

Post by doublemax »

The loop just doesn't work this way. The first parameter to GetFirstChild/GetNextChild must remain the same, but with "child = tv->GetNextChild(child, cookie);" you immediately change it.

Check the method "GetItemsRecursively" from the "treectrl" sample which shows how it works.
Use the source, Luke!
Post Reply