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: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds