Click here to Skip to main content
15,748,098 members
Articles / Desktop Programming / MFC
Posted 27 Apr 2011


106 bookmarked

Extended CComboBox

Rate me:
Please Sign up or sign in to vote.
4.81/5 (40 votes)
10 Dec 2015MIT4 min read
A combobox control which shows a drop down window to assist the user.

Mode: Standard


I often need a combobox that makes suggestions about what I intend to write, but I didn't find anything that fits my needs, so I decided to implement one myself and share it. I tried to keep it simple, standard and easy to use.


My class, CComboBoxExt (ComboBox Extended) is derived from a standard CComboBox (one reason for this solution is to keep the standard look and behaviour), where I use an internal CItemData class to keep and handle all combobox item data:

class CItemData : public CObject
// Attributes
DWORD m_dwItemData;
CString m_sItem;
BOOL m_bState;
// Implementation
CItemData(DWORD dwItemData, LPCTSTR lpszString, BOOL bState);
virtual ~CItemData();

The internal functionality is solved in the following way: every time the user adds a string into the combobox, it is created as a new CItemData item, which keeps all the information about the combobox item. When the user deletes a string from the combobox, it deletes the CItemData object associated with it. When the user types the letters inside of edit, the combobox deletes the items that don't fit with the typed letters, but only from the combobox, not from the CItemData associated object.

Another particularity is that the list of the combobox is subclassed:

void CComboBoxExt::PreSubclassWindow() 
 // TODO: Add your specialized code here and/or call the base class
 COMBOBOXINFO cbi = {0};
 cbi.cbSize = sizeof(COMBOBOXINFO);
 BOOL bRet = SendMessage(CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbi);
 if(bRet && NULL != cbi.hwndList)

m_ListBox is of type CComboBoxExtList, derived from CListBox.


ComboBoxExt can operate in three different ways:

  • MODE_STANDARD, where the list functions like a standard CComboBox control.
  • MODE_DROPDOWN, where combobox shows a dropdown list with the items that begin with letters typed in the edit box.

    Mode: DropDown

    This working mode, MODE_DROPDOWN, can be activated through the CComboBoxExt::SetMode(CComboBoxExt::MODE_DROPDOWN); method.

  • MODE_AUTOCOMPLETE where combobox shows a dropdown list that fits with the letter typed in the edit box, and autocompletes the word that fits with letters typed inside of the edit box.

    Mode: AutoComplete

    This working mode, MODE_AUTOCOMPLETE, can be activated through the CComboBoxExt::SetMode(CComboBoxExt::MODE_AUTOCOMPLETE); method.

The control can adjust the drop down list to the longest item inserted, a feature available in the CComboBoxExt::AdjustDroppedWidth(); method.

Also, the color of the edit box text or background can be changed with CComboBoxExt::AlertBkg(); and CComboBoxExt::AlertText(); methods.

Another feature of CComboBoxExt is to show a tooltip text on the list and edit. This tooltip can be configured in order be shown in several ways:
to show the item text, when the item don't fit the combo, or to show an different text than item text. In order to have another tooltip text than item text, you have few
methods to setup this tooltip text:

virtual int AddStringWithInfo(LPCTSTR lpszString, LPCTSTR lpszInfo, BOOL bShowItemTooltip = TRUE);<br />virtual int InsertStringWithInfo(int nIndex, LPCTSTR lpszString, LPCTSTR lpszInfo, BOOL bShowItemTooltip = TRUE);<br />virtual void SetLBInfo(int nIndex, LPCTSTR lpszInfo, BOOL bShowItemTooltip = TRUE);<br />virtual void GetLBInfo(int nIndex, LPCTSTR lpszInfo) const;<br />virtual void GetLBInfo(int nIndex, CString& rInfo) const;<br />virtual void SetLBShowItemTooltip(int nIndex, const BOOL bShow = TRUE);<br />virtual BOOL GetLBShowItemTooltipState(int nIndex) const;<br />virtual int FindInfo(int nStartAfter, LPCTSTR lpszString) const;
virtual int FindInfoExact(int nIndexStart, LPCTSTR lpszFind) const;
virtual int SelectInfo(int nStartAfter, LPCTSTR lpszString);

you can even find or select an combo item by tooltip info text. Another feature is that you can setup tooltip item in order to being shown at item level, I mean you can say which item show tooltip, and which item will not show tooltip. Also, you can setup the position of tooltip: over item or right alongside him:

void SetEditTooltip(const BOOL bShowTooltip, BOOL bTooltipOnInfo = FALSE, BOOL bShowEditTooltipOverItem = FALSE)<br />void SetListTooltip(const BOOL bShowTooltip, BOOL bTooltipOnInfo = FALSE, BOOL bShowListTooltipOverItem = FALSE)

as you can see, you can setup edit combo tooltip, and/or list combo tooltip separately.

This combination of tooltip configuration can be seen in demo project: combobox with listbox tooltip in alongside:

Image 4

and combobox with list info tooltip over listbox items:

Image 5

Image 6

When you setup the control in order to had tooltip over edit, you can also setup where the tooltip can be shown: right over edit boxes, or above:

Image 7

Image 8

This can be done by using CComboBoxExt::SetEditTooltipOverItemPosition(const BOOL bAbove = TRUE).

Using the control


In order to use this control, you can simply add the ComboBoxExt.h, ComboBoxExt.cpp, ComboBoxExtList.h, and ComboBoxExtList.cpp files in your project, and where you want to use this control, just type #include "ComboBoxExt.h". This control can be used as a dynamically (or not) created control.

Demo project

The demo project includes an SDI demo application which uses the CComboBoxExt control. The working mode of CComboBoxExt could be changed from the radio buttons on the test application form (there you have some tooltips which explain the working mode), and the color of the edit box can be toggled through the Alert text and Alert background buttons from the form.

Known issues

Because of the way CBN_EDITCHANGE is reflected, when you are going to use this control inside of CDialogBarr, in order to use MODE_AUTOCOMPLETE, you must treat ON_CBN_EDITCHANGE inside of your derived CDialogBar class. Example:

// CYourDlgBar header
class CYourDlgBar : public CDialogBar
// Generated message map functions
afx_msg void OnEditchangeCombo();
and in your implementation file:
// CYourDlgBar .cpp file

void CYourDlgBar::OnEditchangeCombo() 
// TODO: Add your control notification handler code here
// Do nothing

At the end ...

An actual implementation opens up new possibilities to customize the dropdown list of the control ... that I hope is coming next ...

Enjoy it!


  • 26th April, 2011: Initial version.
  • 13th July, 2011: Article updated.
  • 10th October, 2011: Updated control archive.
  • 10th May, 2013: Updated control archive.
  • 10th June 2013: Bug solved: when the list was spread up, and became shorter, the list didn't stay closed by edit.
  • 29 August 2013: Updated the drop down list working mode.
  • 11 November 2013: Dropdown list of the control is subclassed.
  • 28 January 2014: Added info tooltip, updated control archive.
  • 17 February 2014: Solved bug: on Win7 and control has style, tooltip is flickering
  • 21 March 2014: Updated and simplified the code.
  • 11 December 2015: The tooltip does not appear when is compiled with VS2008; Bug fixed.
  • 12 December 2022: The tooltip does not appear when is compiled as UNICODE; Bug fixed.
  • 14 February 2023: The control throws errors when create dynamically: Bug fixed.


This article, along with any associated source code and files, is licensed under The MIT License

Written By
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralRe: Prehistoric MFC Pin
EmoBemo11-Nov-14 23:35
EmoBemo11-Nov-14 23:35 
QuestionMouse cursor disappeared Pin
magicpapacy1-May-12 21:14
magicpapacy1-May-12 21:14 
AnswerRe: Mouse cursor disappeared Pin
Davide Zaccanti9-May-12 9:16
Davide Zaccanti9-May-12 9:16 
GeneralRe: Mouse cursor disappeared Pin
magicpapacy9-May-12 14:55
magicpapacy9-May-12 14:55 
Suggestion"Short and sweet" article is hard to understand Pin
David Crow18-Jul-11 8:58
David Crow18-Jul-11 8:58 
GeneralRe: "Short and sweet" article is hard to understand Pin
_Flaviu18-Jul-11 19:35
_Flaviu18-Jul-11 19:35 
QuestionA few function prototypes neeed to be changed to make your code compile under VS8 Pin
Dezhi Zhao13-Jul-11 17:20
Dezhi Zhao13-Jul-11 17:20 
AnswerRe: A few function prototypes neeed to be changed to make your code compile under VS8 Pin
_Flaviu14-Jul-11 2:39
_Flaviu14-Jul-11 2:39 
Thank you for feedback, I'll make the modification. ( I never used VS2008 ).
GeneralRe: A few function prototypes neeed to be changed to make your code compile under VS8 Pin
Dezhi Zhao14-Jul-11 5:50
Dezhi Zhao14-Jul-11 5:50 

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.