Click here to Skip to main content
15,867,141 members
Articles / Web Development / HTML

A Checkbox Tree Control for Use in Filtering Selections

Rate me:
Please Sign up or sign in to vote.
4.50/5 (12 votes)
23 Feb 20032 min read 125.4K   3.5K   58   11
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#)

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:

C++
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 sent. This is where we will capture the mouse again.

C++
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.


Written By
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#? Pin
Sparehawk12-Jul-11 1:47
Sparehawk12-Jul-11 1:47 
GeneralMy vote of 5 Pin
Global Analyser5-Nov-10 0:41
Global Analyser5-Nov-10 0:41 
QuestionCan someone help me fix this code? I get errors! Pin
David Mark Bell6-Sep-09 8:11
David Mark Bell6-Sep-09 8:11 
QuestionLanguage Related Pin
Member 22265221-May-09 11:52
Member 22265221-May-09 11:52 
QuestionCompiled solution ... ??? Pin
Nima Sharifat3-Sep-08 22:47
Nima Sharifat3-Sep-08 22:47 
GeneralC# .NET 2.0 Pin
RodrigoAntunes17-Apr-07 10:48
RodrigoAntunes17-Apr-07 10:48 
QuestionMethods Pin
braveheartkenya27-Dec-05 6:42
braveheartkenya27-Dec-05 6:42 
Generalneed the dll Pin
davemack5-Apr-04 5:39
davemack5-Apr-04 5:39 
I don't have c++ loaded on my machine (nor do I have any desire to). I'd like to get the quickfilter.dll so I can use it in a VB project, but it isn't included in the download. Can you email it to me or make available in the download?

Thanks much.

Dave
Generalright to left Pin
AHMAD COM14-Jan-04 19:15
AHMAD COM14-Jan-04 19:15 
GeneralQuestions Pin
SUPER_ZORRO13-May-03 22:21
SUPER_ZORRO13-May-03 22:21 
GeneralRe: Questions Pin
Magerusan Grigore Cosmin14-May-03 1:52
Magerusan Grigore Cosmin14-May-03 1:52 

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

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