Get next item in outline


Imagine that the outline is completely expanded. Pressing the down key will take you down on line to the next item. Now imagine that instead of expanding the outline and pressing the down key, you want to get to the items in the same sequence programmatically. Although the CTreeView control provides a GetNextItem() function, it does not provide direct support for our needs. Listed below is an overloaded version of GetNextItem() taking a single argument. Before we actually look at the function lets first look at the declaration of the function in the CTreeView derived class.
 
        HTREEITEM GetNextItem( HTREEITEM hItem, UINT nCode ){
                return CTreeCtrl::GetNextItem( hItem, nCode );
        }
        HTREEITEM GetNextItem( HTREEITEM hItem);
It is important that we override the original function taking two arguments otherwise our new version of the function will hide it. Now here is the listing of the overloaded GetNextItem() function.
 
// GetNextItem  - Get next item as if outline was completely expanded
// Returns      - The item immediately below the reference item
// hItem        - The reference item
HTREEITEM CTreeCtrlX::GetNextItem( HTREEITEM hItem )
{
        HTREEITEM       hti;

        if( ItemHasChildren( hItem ) )
                return GetChildItem( hItem );           // return first child
        else{
                // return next sibling item
                // Go up the tree to find a parent's sibling if needed.
                while( (hti = GetNextSiblingItem( hItem )) == NULL ){
                        if( (hItem = GetParentItem( hItem ) ) == NULL )
                                return NULL;
                }
        }
        return hti;
}



Comments

  • Thanks

    Posted by Legacy on 07/05/2003 12:00am

    Originally posted by: Gopobandhu

    your code is very help to me.

    Thanks for it.

    Reply
  • improved version

    Posted by Legacy on 03/15/1999 12:00am

    Originally posted by: Jim Dill

    I found that it was possible for the tree to return ItemHasChildren true, but GetChildItem returned zero,
    causing the walk to stop partway through the tree.  Here's a version which does this case right:
    
    

    HTREEITEM CSTreeCtrl::GetNextItem(HTREEITEM hItem)
    {
    HTREEITEM hti = NULL;

    if (m_ctrl->ItemHasChildren(hItem))
    hti = m_ctrl->GetChildItem(hItem);

    if (hti == NULL) {
    while ((hti = m_ctrl->GetNextSiblingItem(hItem)) == NULL) {
    if ((hItem = m_ctrl->GetParentItem(hItem)) == NULL)
    return NULL;
    }
    }
    return hti;
    }

    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 …

  • Live Event Date: October 29, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this eSeminar, Gene Kim will discuss these survey findings and will share woeful tales of artifact management gone wrong! Gene will also share examples of how …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds