Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / MFC
Article

Case-sensitive ComboBox

Rate me:
Please Sign up or sign in to vote.
4.38/5 (10 votes)
20 Apr 2007CPOL5 min read 175.7K   3K   36   19
CComboBox (and CListBox) with case-sensitive searching

ComboBoxCS Demo

When searching for strings in Combo boxes, the search is always case-insensitive. I have a need for a case-sensitive combo box in an application, so I have put together this class to implement that.

I could have used CComboBoxEx to give me a case-sensitive ComboBox, but it wouldn't be a drop-in replacement for a CComboBox. The methods normally used in CComboBox (eg AddString), are not implemented in CComboBoxEx. This would mean a lot of re-coding in order to use it.

How to use it

Using the CComboBoxCS class is very straightforward. Follow the steps below to add one to an existing project.

  1. After putting the source files (ComboBoxCS.cpp and ComboBoxCS.h) into the directory you wish to use them from, add the files to your Visual Studio project.
  2. In the resource editor, add a combo where you wish.
  3. In Class Wizard add a member variable for your combo control, selecting "Control" from the "Category" list, and selecting "CComboBoxCS" from the "Variable Type" list. (If CComboBoxCS does not appear in the list, you may need to delete your class wizard file (.clw) and regenerate it). I will assume your control variable name is m_combo.
  4. In order to make the combo case-sensitive, add a handler for WM_INITDIALOG in your dialog class if you don't already have one, and add the following code to it:
  5. C#
    m_combo.SetCaseSensitiveSearch();

That's all you need to do.

If you use many Combo boxes which you wish to make them all case-sensitive, you can set the default for all instances by adding:

m_combo.SetCaseSensitiveSearchDefault();

or alternatively, if you always want this behaviour, you can change the default value in the source code:

/*static*/ BOOL CListBoxCS::ms_bCaseSensitiveSearchDefault = TRUE;

How it works

The list box part of CComboBoxCS is subclassed to a CListBoxCS, which provides the case-sensitivity. (Note that the CListBoxCS class can be used in place of a CListBox should you require a case-sensitive list box). In order to subclass the list box, there are one or two methods available. One method ('the messy method') is to handle the WM_CTLCOLOR message. This is a standard (and well-documented) method. The other method ('the clean method') is to use the API function GetComboBoxInfo to ask the combobox for its list control. The drawback with the clean method is that it is only available on Win98, or WinNT 4.0 SP6, or later. The code uses LoadLibrary/GetProcAddress to determine whether the function is available. If it is it uses it, else it falls back to the messy method.

One problem with the messy method, is that the list box doesn't get subclassed until the user drops the list portion of the combo down. This is not good enough here, as we need the list to have been subclassed at the time we do a FindString or FindStringExact. So, we have to cheat.

On receipt of a FindString, FindStringExact, or SelectString message, we look to see whether we need to subclass the list. We need to if we are case-sensitive, and the list has not yet been subclassed. If we don't need to, then we simply pass the message on to the standard combobox Windows procedure.

Assuming we do need to subclass, we then call the function to drop the list portion of the combo. This forces the list to be (re)painted, which causes the required WM_CTLCOLOR to be sent to the combobox.

A problem with this is that we get the list box flicking up, and then closing again. To stop the list flickering, we can't just turn off redraw for the combo, as that doesn't affect the list, nor can we turn if off for the list, as we don't have it yet. The code I have used shrinks the combo so its list box is zero pixels high, and then drops it down. It is then resized to where it came from. This can all be done while the combo has redrawing turned off.

Using the clean method we don't have any of these problems, and can simply ask the combobox for a handle to the list box, which we then subclass. This is done at the earliest time it can be, which will be either as the user types in the edit control, as the list is dropped, or when the combobox receives a FindString/FindStringExact/SelectString message.

History

Version 2.2.1 - 20 Apr 2007

  • Updated to build correctly in VC7 (VS2003) and VC8 (VS2005)

Version 2.2 - 23 Jun 2004

  • Renamed FindString() method to OnFindString(), as this was preventing the use of the standard CComboBox::FindString method
  • added trap for owner-draw messages in the subclassed list to make sure they are handled by the combo-box, to resolve problems with using the combo with owner-draw, which in turn made the subclassed list owner-drawn
  • made the loading of USER32.DLL to provide GetComboBoxInfo() function happen once only for the life of the app, to prevent it happening each time a combo-box is sub-classed
  • changed handlers for CBN_EDITCHANGE and CBN_DROPDOWN to use ON_CONTROL_REFLECT_EX, to allow the control's parent to handle them also
  • added handler for LB_SELECTSTRING to CListBoxCS

Version 2.1 - 09 Jul 2003

  • Updated to support Unicode

Version 2 - 12 Jun 2002

  • new (clean) method of subclassing - uses API function GetComboBoxInfo() if available, else falls back to 'messy' method
  • added handling of CB_SELECTSTRING message
  • minor changes to the searching code

Version 1 - 11 Sep 2001

  • First version.

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)
United Kingdom United Kingdom
Originally from an electronics background, I moved into software in 1996, partly as a result of being made redundant, and partly because I was very much enjoying the small amount of coding (in-at-the-deep-end-C) that I had been doing!

I swiftly moved from C to C++, and learned MFC, and then went on to real-time C on Unix. After this I moved to the company for which I currently work, which specialises in Configuration Management software, and currently program mainly in C/C++, for Windows. I have been gradually moving their legacy C code over to use C++ (with STL, MFC, ATL, and WTL). I have pulled in other technologies (Java, C#, VB, COM, SOAP) where appropriate, especially when integrating with third-party products.

In addition to that, I have overseen the technical side of the company website (ASP, VBScript, JavaScript, HTML, CSS), and have also worked closely with colleagues working on other products (Web-based, C#, ASP.NET, SQL, etc).

For developing, I mainly use Visual Studio 2010, along with an in-house-designed editor based on Andrei Stcherbatchenko's syntax parsing classes, and various (mostly freeware) tools. For website design, I use Dreaweaver CS3.

When not developing software, I enjoy listening to and playing music, playing electric and acoustic guitars and mandolin.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ruslan Iushchenko9-Nov-15 21:14
Ruslan Iushchenko9-Nov-15 21:14 
GeneralGreat! Pin
PatrikE2-May-13 22:40
PatrikE2-May-13 22:40 
GeneralLicense Concern Pin
EstelD20-Jul-10 8:51
EstelD20-Jul-10 8:51 
GeneralRe: License Concern Pin
Paul Vickery23-Aug-10 5:17
professionalPaul Vickery23-Aug-10 5:17 
GeneralCComboBox::FindString() case sensitive possible.. how see the details.. :) Pin
cool_jay23-Jul-05 6:51
cool_jay23-Jul-05 6:51 
GeneralRe: CComboBox::FindString() case sensitive possible.. how see the details.. :) Pin
mustafaguler14-Aug-07 22:52
mustafaguler14-Aug-07 22:52 
GeneralNo need subclassing list of combo Pin
Yury Goltsman14-Jun-02 9:28
Yury Goltsman14-Jun-02 9:28 
GeneralRe: No need subclassing list of combo Pin
Paul Vickery23-Jun-04 3:23
professionalPaul Vickery23-Jun-04 3:23 
GeneralDrop down list auto-selects wrong item Pin
6-Jun-02 5:35
suss6-Jun-02 5:35 
GeneralRe: Drop down list auto-selects wrong item Pin
Paul Vickery9-Jun-02 23:54
professionalPaul Vickery9-Jun-02 23:54 
GeneralRe: Drop down list auto-selects wrong item Pin
Member 726358-Aug-02 15:46
Member 726358-Aug-02 15:46 
GeneralRe: Drop down list auto-selects wrong item Pin
Paul Vickery8-Aug-02 22:23
professionalPaul Vickery8-Aug-02 22:23 
GeneralRe: UNSORTED Drop down list wrong item Pin
12-Aug-02 14:11
suss12-Aug-02 14:11 
GeneralRe: UNSORTED Drop down list wrong item Pin
Member 7263515-Aug-02 12:21
Member 7263515-Aug-02 12:21 
GeneralRe: UNSORTED Drop down list wrong item Pin
Paul Vickery28-Aug-02 0:14
professionalPaul Vickery28-Aug-02 0:14 
GeneralRe: UNSORTED Drop down list wrong item Pin
David Pritchard4-Nov-11 5:41
David Pritchard4-Nov-11 5:41 
QuestionNot implemented? Pin
Uwe Keim24-Feb-02 22:40
sitebuilderUwe Keim24-Feb-02 22:40 
AnswerRe: Not implemented? Pin
Paul Vickery24-Feb-02 23:04
professionalPaul Vickery24-Feb-02 23:04 
GeneralRe: Not implemented? Pin
Uwe Keim24-Feb-02 23:06
sitebuilderUwe Keim24-Feb-02 23:06 

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.