Click here to Skip to main content
Click here to Skip to main content

MultiSelection Tree Control

, 27 Oct 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
This tree control allows the user to select multiple tree items and enable selection through rubber banding.

Introduction

This tree control is a multl-select tree control. This allows the user to select items via rubber band selection. Even though there are a lot of multi-select tree controls published and floating in the web, I tried to make one that was simple to use.

Using the code

The user can simply use it like the CTreeCtrl. The CTreeCtrl can be replaced with the CCustomTreeCtrl. This allows some methods to change the style of the tree control like AddLineStyle(), AddLineAtRoot(), etc. It has a callback facility which calls the function when looping through the items. To loop through the items, a special function is provided, namely IterateItems(). This function takes the callback function as one of the parameters. The user can specify the start and end item for looping.

/****************************************************************************
/DESCRIPTION:
/-----------
/        To iterate all the items and call the callback function for each item
/
/PARAM
/------
/        func[in]    -    Call back function to be called on each item
/        hStart[in]    -    Start node for the iteration
/        hEnd[in]    -    End node to stop the iteration
/        pInfo[in]    -    User parameter
/
/RESULT:
/-------
/        void
*/
void CCustomTreeCtrl::IterateItems( ScanCallBackFunc func,
                            HTREEITEM hStart /*= NULL*/,
                            HTREEITEM hEnd, /*= NULL*/
                            void* pInfo /*=NULL*/
                            )
{
    //If there is no start then take the root item
    if( !hStart )
        hStart = GetRootItem();

    ScanItems( func,hStart,hEnd,pInfo );
}
/***************************************************************************
/DESCRIPTION:
/-----------
/        To iterate all the items and call the callback function for each item
/
/PARAM
/------
/        func[in]    -    Call back function to be called on each item
/        hStart[in]    -    Start node for the iteration
/        hEnd[in]    -    End node to stop the iteration
/        pInfo[in]    -    User parameter
/
/RESULT:
/-------
/        void
*/
void CCustomTreeCtrl::ScanItems( ScanCallBackFunc func,
                            HTREEITEM hStart /*= NULL*/,
                            HTREEITEM hEnd, /*= NULL*/
                            void* pInfo /*= NULL*/
                            )
{
    
    //Loop from start till the end is reached
    while( hStart )
    {
        LOOPINFO lInfo;
        lInfo.pTree =  this;
        lInfo.hItem = hStart;
        lInfo.pParent = GetParent();
        lInfo.param   = pInfo;

        CRect rc;
        GetItemRect( hStart,&rc,FALSE );

        //Callback is called for the current item
        func( &lInfo );

        //Check whether we have reached iterating upto the end node
        if( hStart == hEnd )
            return;

        //Get the childern of the current item and call recursively
        HTREEITEM hChild = NULL;
        if( ItemHasChildren( hStart ) )
        {
            hChild = GetChildItem( hStart );
            ScanItems( func,hChild,hEnd,pInfo);
        }

        hStart = GetNextSiblingItem( hStart );
    }
}

Points of Interest

I have learnt a lot of new things while customizing the tree control, like how to create the drag image if there is multiple selection. I have taken some sample code from CodeProject articles to accomplish this. But I think I have made the multiple selection quite easy and I have provided lots of comments which will be very helpful to understand the code.

History

This is the first version of the code. Based on the user feedback and bug reports, I'll update the code and try to deliver the best. Please feel free to send your feedback.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Babu_Abdulsalam
Software Developer
India India
I'm working as Senior software Engineer since 7 years and interested in MFC and COM programming.

Comments and Discussions

 
QuestionDoes not work for me... PinmemberDLiviu16-Apr-14 2:01 
AnswerRe: Does not work for me... Pinmembersyed_babu4-Jun-14 22:34 
GeneralRe: Does not work for me... PinmemberDLiviu5-Jun-14 1:33 
QuestionMore stable version Pinmembersyed_babu3-Feb-13 17:12 
Refer Versatile Tree Control[^] for multiselection tree control with lot more new features.
AnswerRe: More stable version PinmemberDLiviu30-Apr-14 2:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 28 Oct 2008
Article Copyright 2008 by Babu_Abdulsalam
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid