Expanding a branch


The treeview has built in support for expanding expanding the outline one level at a time. If you want to completely expand a branch, the code below will help. It uses recursion to expand all items. The last line, that is, the call to EnsureVisible() is useful when this code is hooked up to a user interface. After expanding the outline branch, the previously selected item may have scrolled off and calling EnsureVisible() brings it back.
 
// ExpandBranch - Expands a branch completely
// hti          - Handle of the tree item to expand
void CTreeCtrlX::ExpandBranch( HTREEITEM hti )
{
        if( ItemHasChildren( hti ) ){
                Expand( hti, TVE_EXPAND );
                hti = GetChildItem( hti );
                do{
                        ExpandBranch( hti );
                }while( (hti = GetNextSiblingItem( hti )) != NULL );
        }
        EnsureVisible( GetSelectedItem() );
}



Comments

  • How do I check if the branch is expanded?

    Posted by Legacy on 06/19/2002 12:00am

    Originally posted by: Todd

    When I do drag and drop I want to redraw the items in another branch, the problem is that when I redraw the other branches they collapse. Is there a way to check if an item is expanded so I can expand the branches that the user had selected before the redraw?

    Reply
  • For the people who like recursion

    Posted by Legacy on 02/25/2002 12:00am

    Originally posted by: Amir Geva

    Here is a simple recursive no loop implementation:
    
    

    void MyTreeCtrl::ExpandAll(HTREEITEM item, UINT nCode=TVE_EXPAND, bool siblings=false)
    {
    if (item!=NULL)
    {
    Expand(item,nCode);
    ExpandAll(GetChildItem(item),nCode,true);
    if (siblings) ExpandAll(GetNextSiblingItem(item),nCode,true);
    }
    }

    Reply
  • Expanding/Collapsing with numeric pad

    Posted by Legacy on 01/28/2000 12:00am

    Originally posted by: Jean Claude DAUPHIN

    It seems to be a Tree Ctrl UI standard feature:

    Keyboard "*","+" on numeric pad allow to expand the branch
    under the selected item.
    Keyboard "-" on numeric pad allow to collapse the branch under the selected item.

    Reply
  • Another way

    Posted by Legacy on 07/24/1999 12:00am

    Originally posted by: Ant Htoo Naing

    U can use this method if you don't use VK_MULTIPLY( "*" key from Numeric Key Pad) for another purpose.
    
    

    void CTreeCtrlEx::ExpandBranch(HTREEITEM hItem)
    {
    HTREEITEM hSelectItem=GetSelectedItem();

    SelectItem(hItem);
    SendMessage(WM_KEYDOWN,VK_MULTIPLY);

    if (hSelectItem==NULL)
    hSelectItem=GetRootItem();
    EnsureVisible(hSelectItem);
    SelectItem(hSelectItem);
    }

    Reply
  • How about change like this?

    Posted by Legacy on 07/10/1999 12:00am

    Originally posted by: Eung-Yeon, Kim

    I think the bellow code occurs some problem when hti is NULL
    
    

    => if( ItemHasChildren( hti ) ){

    Because hti may be NULL if The tree has not Child Item

    => hti = GetChildItem(hti);


    So, How about rewrite the code like bellow

    // ExpandBranch - Expands a branch completely
    // hti - Handle of the tree item to expand
    void CTreeCtrlX::ExpandBranch( HTREEITEM hti )
    {
    => if( ItemHasChildren( hti ) && hti != NULL){
    Expand( hti, TVE_EXPAND );
    hti = GetChildItem( hti );
    do{
    ExpandBranch( hti );
    }while( (hti = GetNextSiblingItem( hti )) !
    = NULL );
    }
    EnsureVisible( GetSelectedItem() );
    }

    Reply
  • Memory fault!!

    Posted by Legacy on 06/08/1999 12:00am

    Originally posted by: Seo-Wooseok

    When I used this way, Windows98 send 'memory fault' message.
    
    

    Because my TreeCtrl has so many tree item. So, Windows98's memory overflowed. I think so.

    Then, how can I solve this problem?

    Thank you for reading..

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • With the average hard drive now averaging one terabyte in size, the fallout from the explosion of user-created data has become an overwhelming volume of potential evidence that law-enforcement and corporate investigators spend countless hours examining. Join Us and SANS' Rob Lee for our 45-minute webinar, A Triage and Collection Strategy for Time-Sensitive Investigations, will demonstrate how to: Identify the folders and files that often contain key insights Reduce the time spent sifting through content by …

Most Popular Programming Stories

More for Developers

RSS Feeds