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

  • This paper introduces IBM Java on the IBM PowerLinux 7R2 server and describes IBM's implementation of the Java platform, which includes IBM's Java Virtual Machine and development toolkit.

  • In the competitive marketplace that surrounds us today, customers shouldn't have to settle for legacy desktop or application delivery simply because they've relied on a certain vendor in the past. This white paper reviews how three customers decided to partner with VMware, and how they benefited from the latest VDI and app trends to improve the end-user experience, increase productivity, reliability and stability to deliver better SLAs - with lower cost and less time needed to manage end users.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds