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

  • Instead of only managing projects organizations do need to manage value! "Doing the right things" and "doing things right" are the essential ingredients for successful software and systems delivery. Unfortunately, with distributed delivery spanning multiple disciplines, geographies and time zones, many organizations struggle with teams working in silos, broken lines of communication, lack of collaboration, inadequate traceability, and poor project visibility. This often results in organizations "doing the …

  • The rapid evolution of enterprise storage technologies, combined with external forces, like the explosion of big data, can cause Linux® and server administrators to play catch-up when it comes to storage. Running a bunch of monolithic storage devices and proprietary, disconnected technologies forces administrators to spend valuable time creating and managing complex solutions. To reduce complexity and enable rapid deployment of new technologies and applications, server administrators need a single open …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds