Get last item in branch


In my programming, I have often had to derive the last item in a branch. In fact some of the other topics I cover, uses this function to get the last item in the branch.
 
// GetLastItem  - Gets last item in the branch
// Returns      - Last item
// hItem        - Node identifying the branch. NULL will 
//                return the last item in outine
HTREEITEM CTreeCtrlX::GetLastItem( HTREEITEM hItem )
{
        // Last child of the last child of the last child ...
        HTREEITEM htiNext;

        if( hItem == NULL ){
                // Get the last item at the top level
                htiNext = GetRootItem();
                while( htiNext ){
                        hItem = htiNext;
                        htiNext = GetNextSiblingItem( htiNext );
                }
        }

        while( ItemHasChildren( hItem ) ){
                htiNext = GetChildItem( hItem );
                while( htiNext ){
                        hItem = htiNext;
                        htiNext = GetNextSiblingItem( htiNext );
                }
        }

        return hItem;
}



Comments

  • Little problem ???

    Posted by Legacy on 11/05/1998 12:00am

    Originally posted by: Patrice Lacroix

    I believe I found a problem in this method.
    
    

    Please correct me if I'm wrong or if I am not using this method correctly.

    If I am searching for a string in the tree and this string is
    currently selected, the method will return NULL.

    Should I check before I call the method ?

    Here is what I did to correct it:

    // For the first pass, set the current item equal to the selection
    HTREEITEM htiSel = hItem ? hItem : GetSelectedItem();
    HTREEITEM htiCur = htiSel;

    ...
    ...
    // For the first pass only, we check to see if it
    // is the item we're looking for.
    BOOL bFirstPass = TRUE;

    while( htiCur && (htiCur != htiSel || bFirstPass) )
    {
    bFirstPass = FALSE;
    ...
    ...
    ...
    }

    ...
    ...
    ...

    Please, tell me if you have a better way to do it.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • New IT trends to support worker mobility — such as VDI and BYOD — are quickly gaining interest and adoption. But just as with any new trend, there are concerns and pitfalls to avoid.  Download this paper to learn the most important considerations to keep in mind for your VDI project.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds