Example of the new ListView FilterBar feature (IE5)

Environment: Visual C++ 6, IE 5

This example program and code show you one of the ways to use the new, mostly undocumented, FilterBar feature of the ListView control that is available with IE5.0 (or greater). While the example application works, it is by no means an optimal example of how to use this new feature(!).

The Example Application will most likely better explain how to use the FilterBar feature, as the important code is, IMHO, very well commented. Please look it over first if you have any questions. (Tip: Set your Tab size to 4!) FilterBar Example 1 FilterBar Example 2

With IE5.0 comes a new feature for the ListView Control: the FilterBar. An example of how to use this new feature is in the example application (see above). For more information browse through your CommCtrl.h file. (Actually, this is a very smart thing to do each time you install a new version of the SDK! You might be suprised what you can find! This FilterBar stuff is one example, there is also a new Rebar style! Check it out for yourself!)

Using this new feature is simple. First, apply the HDS_FILTERBAR style to the ListView control's Header control. This tells the ListView's Header control to show the FilterBar (filter controls):


m_lcList.GetHeaderCtrl() -> ModifyStyle( 0, HDS_FILTERBAR );			// Add The FilterBar Style
Then, specify the Filter Types for the Columns. In the following example code, I set the Filter Type to HDFT_ISSTRING, which is the String filter type:

HDITEM	hdItem;
hdItem.mask = ( HDI_FILTER );							// Set Mask For Filter Type Information
hdItem.type = HDFT_ISSTRING;							// Set String Filter Type
pHeader -> SetItem( 0, &hdItem );						// Set Item Information
pHeader -> SetItem( 1, &hdItem );						// Set Item Information
pHeader -> SetItem( 2, &hdItem );						// Set Item Information
Now, use the HDM_SETFILTERCHANGETIMEOUT message to set the Timeout value for Filter Change notifications. This allows the user to type in a value without the application constantly processing Filter Change notifications. On the other hand, if you want to get a notification for each character, you could try setting the Timeout value to a very small number. This is useful for incremental filtering.

m_lcList.GetHeaderCtrl() -> SendMessage( HDM_SETFILTERCHANGETIMEOUT, 0, 1000 );	// Set One Second Timeout (Milliseconds)
All that is left to do is to process the notification messages sent by the FilterBar. These notification messages are:

HDN_FILTERCHANGE                Sent to notify you of changes to the Filter 
HDN_FILTERBTNCLICK              Sent to notify you of the FilterBar button being pressed
See the CommCtrl.h file for more information on the above notification messages, and the structures they use.

To get filter information from the control, you use the HDITEM structure, which now has two additional data members, type and pvFilter. Set the mask member of the HDITEM structure with the HDI_FILTER value to manipulate the FilterBar specific members. The type member is used to specify the Filter type, and can accept the following values:


HDFT_ISSTRING                   Filter type is String 
HDFT_ISNUMBER                   Filter type is Number (int) 
HDFT_HASNOVALUE                 Filter is Empty (Setting this value will clear the existing Filter)
The pvFilter member is used to obtain the value in the filter. If type is set to HDFT_ISNUMBER, pvFilter should point to an int. If type is set to HDFT_ISSTRING, pvFilter is a pointer to an HDTEXTFILTER structure, which itself contains a pointer to a buffer, and the (character) length of the buffer. (Note that I could not get the Number type to work correctly.)

After calling CHeaderCtrl::GetItem(...), the pvFilter (int or HDTEXTFILTER structure pointer) should be populated with the Filter data. What you do with this data is up to you.

In the Example Application, this filter data is used to perform incremental filtering of the set of data, and then the resulting data set, if any, is displayed.

While I am no expert with the FilterBar stuff, I would be more than happy to try to answer any questions about it. Have fun!

Downloads

Download demo application - 40 Kb
Download demo project - 8 Kb
Download source - 16 Kb


Comments

  • HDM_GETITEMW leaks memory with HDI_FILTER

    Posted by itsomne on 04/13/2009 03:46am

    My code seems to be leaking memory. when i use HDM_GETITEMW, any thoughts?
    
    
    HDITEM			hdItem;
    HDTEXTFILTER	hdTFilter;
    TCHAR			cFilter[1024];
    hdItem.mask = ( HDI_FILTER );						// Set Mask
    ZeroMemory( cFilter, sizeof( cFilter ) );			// Init Buffer For Filter
    hdTFilter.pszText = cFilter;						// Set Buffer For Filter
    hdTFilter.cchTextMax = sizeof( cFilter );			// Set Size Of Buffer
    hdItem.type = HDFT_ISSTRING;						// Set String Filter Type
    hdItem.pvFilter = &hdTFilter;			
    ::SendMessage(pHeader -> m_hWnd, HDM_GETITEMW,iColNo, (LPARAM)&hdItem);	// Get Item Filter Data

    Reply
  • It is not working on my system

    Posted by bharatsingh on 03/16/2005 12:26am

    i downloaded this demo project, it is working on my system very well, but when i wrote my own code like this then there are so many errors like HDS_FILTERBAR , HDI_FILTER and hdItem.type = HDFT_ISSTRING not define. there are many other errors like this please helpe my email id is bharat.singh@sisl.co.in

    • Missing SDK...

      Posted by jtwine on 02/03/2006 01:40pm

      You need to have the updated SDK installed correctly to have those values available. -=- James

      Reply
    Reply
  • Filter edit don't get input more that 4 chars if text set programetically?

    Posted by Legacy on 08/09/2002 12:00am

    Originally posted by: Sajid Majid

    Hi All,
    On button click I am creating ListBox & then selecting value to filter. The selected value is set in filter using SetItem(...). This is ok, I can delete some chars from set value but can't enter if more than 4 char in edit. I have found this problem when I have set value for column from list, otherwise u can edit with keyboard without any problem.
    Hope any one have any solution?.

    Regards.
    Majid.

    Reply
  • Access Violation in Comctl32.dll

    Posted by Legacy on 02/19/2002 12:00am

    Originally posted by: Kyle Sluder

    When my program calls pHeader->SetItem() I get an Access Violation in Comctl32.dll... is something going on with SetItem? I'm running VC++ 6 Standard, Win98 SE, IE6, and Office 2000 Pro.

    Also, how do I filter the list? In your demo app (which works without an access violation) I enter a filter into the FilterBar (example: Car) and click on the filter icon, but it doesn't want to work... I still get all the Bucket items, for example.

    Reply
  • Better way...

    Posted by Legacy on 12/06/2001 12:00am

    Originally posted by: Stephan Roth

    To overwrite the virtual method OnNotify() I do not hold for a particularly elegant solution.
    
    The better way (IMHO) is to add ON_NOTIFY message handlers for HDN_FILTERCHANGE and/or HDN_FILTERBTNCLICK:

    In the header file add:

    afx_msg void OnFilterChanged(NMHDR* pNMHDR, LRESULT* pResult);
    afx_msg void OnFilterBtnClicked(NMHDR* pNMHDR, LRESULT* pResult);

    In the .cpp file add the following entries to the message map, but outside(!) of the special comments for the MFC ClassWizard:

    BEGIN_MESSAGE_MAP(MyClass, SuperClass)
    //{{AFX_MSG_MAP(MyClass)
    // .....
    //}}AFX_MSG_MAP

    ON_NOTIFY(HDN_FILTERCHANGE, 0, OnFilterChanged)
    ON_NOTIFY(HDN_FILTERBTNCLICK, 0, OnFilterBtnClicked)

    END_MESSAGE_MAP()

    The functions:

    void MyClass::OnFilterChanged(NMHDR* pNMHDR, LRESULT* pResult)
    {
    LPNMHEADER pNMHeader = reinterpret_cast<LPNMHEADER>(pNMHDR);

    // ..........

    *pResult = 0;
    }

    void MyClass::OnFilterBtnClicked(NMHDR* pNMHDR, LRESULT* pResult)
    {
    LPNMHDFILTERBTNCLICK pNMBClick =
    reinterpret_cast<LPNMHDFILTERBTNCLICK>(pNMHDR);

    // ........

    *pResult = 0;
    }

    Just my two pence,
    Stephan Roth, Germany

    Reply
  • RE:The program can not compile!

    Posted by Legacy on 11/28/2001 12:00am

    Originally posted by: XingJun Zhang

    Thanks Mr James for this free code program.But downloaded the program and cannot get it to compile: 26 errors.

    1 - FBListCtrl.cpp(28) : error C2065: 'HDN_FILTERCHANGE' : undeclared identifier
    2 - FBListCtrl.cpp(29) : error C2065: 'HDN_FILTERBTNCLICK' : undeclared identifier
    etc...

    I have downloaded the latesd SDK and installed it, also I have added the include and lib directory of SDK into the IDE of VC6.

    Thanks!

    Reply
  • Any way to tab b/w filters?

    Posted by Legacy on 06/29/2001 12:00am

    Originally posted by: Keri

    Has anybody discovered a way to allow the user to move b/w filter edits w/o using the mouse? I'd love to get the TAB key to do the trick...

    Reply
  • Code does not compile

    Posted by Legacy on 05/03/2001 12:00am

    Originally posted by: Bill Roach

    Down loaded your source code and cannot get it to compile: 25 errors.
    
    1 - FBListCtrl.cpp(28) : error C2065: 'HDN_FILTERCHANGE' : undeclared identifier
    2 - FBListCtrl.cpp(29) : error C2065: 'HDN_FILTERBTNCLICK' : undeclared identifier
    etc...

    I have Version 5.81 of Comctl32.dll.

    Suggestions.

    Reply
  • Refresh problem when double-clicking a column intersection...

    Posted by Legacy on 10/13/2000 12:00am

    Originally posted by: Benjamin Mayrargue

    There is a refresh problem when you double-click a column intersection in the column header in order to make it resize to its content.

    The column extends to the width of its largest item.
    But the filter button is not erased from its previous position.

    This is in "comctl32.dll" ver 5.81.4134.600
    (product ver 5.50.4134.600)
    from IE 5.5 / NT4

    ___________________________
    http://welcome.to/softbird

    Reply
  • Anyone that is having problems...

    Posted by Legacy on 09/22/2000 12:00am

    Originally posted by: James R. Twine

    Please LMK exactly what version of Windows, IE, and MS Office is installed on your system. I am going to try to find time to find out why this thing is getting wonky on certain systems.

    -=- James.

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds