Click here to Skip to main content
15,867,756 members
Articles / Desktop Programming / MFC
Article

CFilterHeaderCtrl and CFilterListCtrl - Give user a chance to filter data you brought him!

Rate me:
Please Sign up or sign in to vote.
4.79/5 (23 votes)
11 May 2003CPOL2 min read 104.5K   1.7K   65   13
An owner drawn header control and a CListControl that uses it

Sample Image - FilterHeaderCtrl.jpg

Introduction

CListCtrl is my preferred control. Most of the times I use it in 'report' mode, because it's a pretty way to show 'rows of data' that user can easily scroll, sort, view. But often I need a way to let user choose, which data are meaningful for him. You see, there is a nice HDS_FILTERBAR on MSDN... but it was not there when I needed it! So, here is the CFilterHeaderCtrl, that you can use alone or within a CFilterListCtrl.

CFilterHeaderCtrl

This control is derived from a CHeaderCtrl and is owner drawn. Here you can see the notification messages fired from the control to the parent window:

#define    FLCN_FILTERCHANGING 101
#define    FLCN_FILTERCHANGED 102
#define    FLCN_SHOWINGEDIT 103
#define    FLCN_BEGINFILTEREDIT 104
#define    FLCN_ENDFILTEREDIT 105

FLCN_FILTERCHANGING and FLCN_FILTERCHANGED are called before and after a filter is changed by the user. Here you can prevent a filter change. The message handler will receive the following struct:

struct NMFILTERHDR : public NMHDR
{
    char* szText; // read only!!!
};

FLCN_SHOWINGEDIT is called when the edit control is about to be shown.

FLCN_BEGINFILTEREDIT and FLCN_ENDFILTEREDIT are called when the user start editing one or more filters, moving arount with the TAB key. In the sample code, you can see how to track how many filters have been changed and in that moment you can reload the list, without having to reload for every filterchange event.

For every column you add, you can choose one of the following styles:

#define FILTER_NONE                0
#define FILTER_ENABLED             1
#define FILTER_DISABLED            2

CFilterListCtrl

Instead of using the cFilterHeaderCtrl, you will find more useful to replace your CListCtrl with the CFilterListCtrl. The CFilterListCtrl will replace the standard header control with the CFilterHeaderCtrl and will forward to the parent, the notification messages.

Conclusion

I'm sure that you will find more answer to your questions looking around in the sample code. In the sample project you have to add your own code to reload the list!!!

Many many thanx to CodeProject and CodeGuru people for the many many articles on owner drawn and custom control implementation. Have fun!

License

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


Written By
Software Developer (Senior) Leonardo
Italy Italy
Hi Smile | :)
I was born in 1970 (Augusta - Italy).
I live in Taranto - Italy.
I work in Taranto - Italy.
I like computer science!!!
That's all!

Comments and Discussions

 
GeneralReload the List Pin
Oliverikawood7-Sep-10 18:51
Oliverikawood7-Sep-10 18:51 
GeneralBig memory problems Pin
estartu_de8-Mar-04 1:23
estartu_de8-Mar-04 1:23 
GeneralUse the Ctrl in an MDI-App Pin
reiser18-Feb-04 1:34
reiser18-Feb-04 1:34 
GeneralFilter text problem Pin
Davide Zaccanti10-Sep-03 3:20
Davide Zaccanti10-Sep-03 3:20 
GeneralRe: Filter text problem Pin
Massimiliano Conte10-Sep-03 3:38
Massimiliano Conte10-Sep-03 3:38 
GeneralRe: Filter text problem Pin
Davide Zaccanti15-Sep-03 20:48
Davide Zaccanti15-Sep-03 20:48 
GeneralRe: Filter text problem Pin
Massimiliano Conte17-Sep-03 23:39
Massimiliano Conte17-Sep-03 23:39 
GeneralRe: Filter text problem Pin
Vivian De Smedt5-Aug-06 3:52
Vivian De Smedt5-Aug-06 3:52 
GeneralWant to use the control Pin
ajit0727-May-03 0:53
ajit0727-May-03 0:53 
GeneralDefault filter string Pin
dgs23-May-03 20:24
dgs23-May-03 20:24 
QuestionHow to change the default filter string Pin
carlos.sousa11-Oct-05 10:33
carlos.sousa11-Oct-05 10:33 
GeneralThis is Good! Pin
WREY13-May-03 4:57
WREY13-May-03 4:57 
Generalcreative idea, so 5 ponits to you Pin
Zhefu Zhang12-May-03 8:27
Zhefu Zhang12-May-03 8:27 

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.