Click here to Skip to main content
Click here to Skip to main content
Go to top

A checkbox tree control for use in filtering selections

, 23 Feb 2003
Rate this:
Please Sign up or sign in to vote.
An ATL filter control

Sample Image - maximum width is 600 pixels

Introduction

A year ago I posted an MFC control in the article "An Enhanced CCheckComboBox". Many readers asked me to make this control a COM component available from VB or .NET. Another request was to make the addition of new controls as quick as possible. I decided to create an ATL component which contains a collection of combos with checked trees dropdowns. The combos are created dynamically and displayed in 2, 3 or 4 columns.

Using the code

To use this code, first you will have to build the "QuickFilter" project in VC++ 6.0 or VC++ .NET. Then you can run the three demo projects: "Test with VB", "Test with HTML" and "Test with C#".

Example for populating the first combo: (C#)

    //## INIT Countries 
    QF.set_CheckLabel( 0, "Countries:" );
    QF.set_Field( 0, "Countries.ID" );
    QF.AddFolder( 0, "North America" );
        QF.AddString( 0, "USA", 5, ROOT_LEVEL + 2 );
        QF.AddString( 0, "Canada", 6, ROOT_LEVEL + 2 );
    QF.AddFolder( 0, "Europe" );
        QF.AddString( 0, "UK", 7, ROOT_LEVEL + 2 );
        QF.AddString( 0, "Germany", 8, ROOT_LEVEL + 2 );
        QF.AddString( 0, "Russia", 9, ROOT_LEVEL + 2 );
    QF.AddFolder( 0, "Asia" );
        QF.AddString( 0, "Israel", 10, ROOT_LEVEL + 2 );
    QF.CheckAll( 0, true );

Points of Interest

The ComboBox control is made from an EditBox, a Button and a TreeView. The TreeView is a child of the desktop window, and is shown or hidden when the user presses the button or the edit of the combo (this behavior is by design). I no longer subclass the parent window of the combo, as I did in the previous article but instead I use mouse capture and the LostFocus event to hide the dropdown when needed.

When the user presses the button of the combo I show the dropdown and I wait the WM_LBUTTONUP message to capture the mouse. At this point I know that a click outside my control will have to hide the dropdown. Everything seems to be easy, but there is a problem: the scroll bars of the tree dropdown will not work properly if the mouse is already captured. To handle this situation we will release the mouse capture just as long as the user uses the scrolls and then recapture the mouse again:

LRESULT CCheckTreeCtrl::OnLButtonDown(UINT uMsg, WPARAM wParam, 
                                           LPARAM lParam, BOOL& bHandled)
{
    ...
    //## RELEASE Capture
    if (hWnd == m_hWnd)
        if (GetCapture() == hWnd){
            LRESULT result = ::SendMessage( hWnd, WM_NCHITTEST, NULL, 
                                            MAKELONG(point.x, point.y));
            if ((result == HTVSCROLL) || (result == HTHSCROLL)){
                ::ReleaseCapture();
                m_bScroll = TRUE;
                ::SendMessage( hWnd, WM_NCLBUTTONDOWN, result, 
                               MAKELONG(point.x, point.y));
            }
        }
    ...
}

When the user finishes using the scrollbars a WM_CAPTURECHANGED message will be send. This is where we will capture the mouse again.

LRESULT CCheckTreeCtrl::OnCaptureChanged(UINT uMsg, WPARAM wParam, 
                                              LPARAM lParam, BOOL& bHandled)
{
    ...
    //## SET Capture
    if (IsWindowVisible())
        if (lParam == NULL)
            if (m_bScroll){
                SetCapture();
                m_bScroll = FALSE;
            }
    ...
}

The rest is quite straightforward. Enjoy using it!

History

This is the first release.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Magerusan Grigore Cosmin
Web Developer
Romania Romania
In June 2002 Magerusan Grigore Cosmin, took a Master's Degree in Computer Science at UBB University in Cluj.
 
He has ~2 years of experience using in VC++.

Comments and Discussions

 
QuestionHow I can use it in VS2010 C#? PinmemberSparehawk12-Jul-11 1:47 
GeneralMy vote of 5 PinmemberHeaven20205-Nov-10 0:41 
QuestionCan someone help me fix this code? I get errors! PinmemberDavid Mark Bell6-Sep-09 8:11 
QuestionLanguage Related PinmemberMember 22265221-May-09 11:52 
QuestionCompiled solution ... ??? Pinmembernish013-Sep-08 22:47 
GeneralC# .NET 2.0 PinmemberRodrigoAntunes17-Apr-07 10:48 
QuestionMethods Pinmemberbraveheartkenya27-Dec-05 6:42 
Generalneed the dll Pinmemberdavemack5-Apr-04 5:39 
Generalright to left PinmemberAHMAD COM14-Jan-04 19:15 
GeneralQuestions PinmemberSUPER_ZORRO13-May-03 22:21 
GeneralRe: Questions PinmemberMagerusan Grigore Cosmin14-May-03 1:52 

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 | Mobile
Web02 | 2.8.140922.1 | Last Updated 24 Feb 2003
Article Copyright 2003 by Magerusan Grigore Cosmin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid