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

  • On-demand Event Event Date: September 10, 2014 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 how the best mobile …

  • Information is data with context. The era of Big Data has begun demonstrating to information security that there is more that can, and must, be done to identify threats, reduce risk, address fraud, and improve compliance monitoring activities by bringing better context to data and thereby creating information for actionable intelligence. This analyst report sets the stage and provides insights into IT and information security practitioners' perceptions of the impediments to, and the solutions necessary for, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds