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: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds