viksoe.dk

Contacts Filtering List

Contacts Filtering List


This article was submitted .


This WTL control simulates the behaviour of the Contact list on the MS Smartphone platform. The neat feature of this list is that it filters the items when you press the keys of the keypad and highlights the matched letters. So when you press 2 on the keypad, only names starting with the letter a, b or c will be displayed (both first-name and sur-name are considered).

The control is a subclassed LISTVIEW control. Two columns are displayed with the control - and this is a fixed number. The second column displays a single character item only.
Internally the control manages its own item list, repopulates the entire list when the filter changes and uses a pattern matching algorithm to filter the items.

Supporting Backspace

The filter is reset when the "Back" key is pressed. However, Smartphone is real picky about the backspace key and you need to shuffle it a bit to get it working. There's an article on the Microsoft web-site that explains it, but basically you need to send a SHCMBM_OVERRIDEKEY message to the Command Bar to override the default behaviour and then handle WM_HOTKEY to relay the key-down message to the active window.

How to use it

To use it, place a LISTVIEW control on your dialog. This control must be created with or have the Fixed Ownerdrawn property set in the resource editor.
Add a member variable to your dialog implementation file...
CFilterListCtrl m_list
In the OnInitDialog() event handler, add the following line:
  LRESULT OnInitDialog(UINT /*uMsg*/, 
                       WPARAM /*wParam*/, 
                       LPARAM /*lParam*/, 
                       BOOL& /*bHandled*/)
  {
    ...
    m_list.SubclassWindow(GetDlgItem(IDC_LIST1));
    ...
  }
Also add this reflection macro to your main message map:
  BEGIN_MSG_MAP(CMainDlg)
    ...
    REFLECT_NOTIFICATIONS()
  END_MSG_MAP()
There's no support for images and sub-items, though the supporting methods have not been disabled.

To insert new items, use the overloaded InsertItem() method.

m_list.InsertItem(_T("Duck, Donald"), _T("D"));
If you need sorting and other of the LISTVIEW control's features, you can simply add the styles as usual to the control.

Use the GetItemData() method to retrieve the original index of an item once it has been selected or while you are enumerating items.

Source Code Dependencies

MS Smartphone 2003
Microsoft WTL 7.5 Library

Download Files

DownloadSource Code (27 Kb)

To the top