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: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds