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: 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 …

  • Mobile is introducing sweeping changes throughout your workplace. As a senior stakeholder driving mobile initiatives in your organization, you may be lost in a sea of technologies and claims from vendors promising rapid delivery of applications to your employees, customers, and partners. To help explain some of the topics you will need to be aware of, and to separate the must-haves from the nice-to-haves, this reference guide can help you with applying a mobile strategy in the context of application …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds