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

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • If you need new tools and tricks to make your meetings profitable and productive, then 5 Tips in 5 Minutes: A Quick Guide for More Profitable Sales Meetings is for you. Timely, practical tips that you can incorporate in just seconds will save you literally hours in travel and meeting time, not to mention help you to focus on what your sales prospects really want to know and how you can meet their needs. Get 5in5: A Quick Guide for More Profitable Sales Meetings and start building your sales the smarter, faster …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds