Click here to Skip to main content
16,020,714 members
Articles / Desktop Programming / MFC

Extended CComboBox

Rate me:
Please Sign up or sign in to vote.
4.82/5 (41 votes)
10 Dec 2015MIT4 min read 168.8K   6.1K   107   59
A combobox control which shows a drop down window to assist the user.

Mode: Standard

Introduction

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.

Description

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
public:
DWORD m_dwItemData;
CString m_sItem;
BOOL m_bState;
// Implementation
public:
CItemData();
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:

C++
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.SubclassWindow(cbi.hwndList);
 ....
 CComboBox::PreSubclassWindow();
}

m_ListBox is of type CComboBoxExtList, derived from CListBox.

Features

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:

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

BEGIN_MESSAGE_MAP(CYourDlgBar, CDialogBar)
//{{AFX_MSG_MAP(CYourDlgBar)
ON_CBN_EDITCHANGE(IDC_COMBO_FILE, OnEditchangeCombo)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
.....
.....
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!

History

  • 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.

License

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

 
BugUsing a pop-up CComboBoxExt Pin
Sesquipedalian4-Feb-23 17:51
Sesquipedalian4-Feb-23 17:51 
GeneralRe: Using a pop-up CComboBoxExt Pin
_Flaviu6-Feb-23 3:59
_Flaviu6-Feb-23 3:59 
GeneralRe: Using a pop-up CComboBoxExt Pin
Sesquipedalian6-Feb-23 14:48
Sesquipedalian6-Feb-23 14:48 
GeneralRe: Using a pop-up CComboBoxExt Pin
_Flaviu12-Feb-23 5:25
_Flaviu12-Feb-23 5:25 
GeneralRe: Using a pop-up CComboBoxExt Pin
Sesquipedalian12-Feb-23 5:33
Sesquipedalian12-Feb-23 5:33 
GeneralRe: Using a pop-up CComboBoxExt Pin
_Flaviu13-Feb-23 23:17
_Flaviu13-Feb-23 23:17 
GeneralRe: Using a pop-up CComboBoxExt Pin
Sesquipedalian14-Feb-23 12:33
Sesquipedalian14-Feb-23 12:33 
QuestionLicense Pin
Sesquipedalian27-Dec-22 19:41
Sesquipedalian27-Dec-22 19:41 
AnswerRe: License Pin
_Flaviu27-Dec-22 19:49
_Flaviu27-Dec-22 19:49 
AnswerRe: License Pin
_Flaviu27-Dec-22 23:06
_Flaviu27-Dec-22 23:06 
GeneralRe: License Pin
Sesquipedalian28-Dec-22 4:16
Sesquipedalian28-Dec-22 4:16 
QuestionCode not working with Unicode character set Pin
Member 1585537611-Dec-22 20:04
Member 1585537611-Dec-22 20:04 
AnswerRe: Code not working with Unicode character set Pin
_Flaviu11-Dec-22 23:57
_Flaviu11-Dec-22 23:57 
GeneralRe: Code not working with Unicode character set Pin
Member 1585537620-Dec-22 22:42
Member 1585537620-Dec-22 22:42 
Questionwhy combobox will select string automatically after i use showdropdown(true) in my code? Pin
TimGallin10-Mar-16 17:52
TimGallin10-Mar-16 17:52 
AnswerRe: why combobox will select string automatically after i use showdropdown(true) in my code? Pin
_Flaviu26-Sep-16 22:17
_Flaviu26-Sep-16 22:17 
QuestionVery handy Pin
vinaypeepl14-Dec-15 21:55
vinaypeepl14-Dec-15 21:55 
AnswerRe: Very handy Pin
_Flaviu14-Dec-15 22:41
_Flaviu14-Dec-15 22:41 
QuestionCan I use this in owner-drawn FIXED ComboBox? Pin
Itban Saeed Usmani11-Aug-15 20:46
professionalItban Saeed Usmani11-Aug-15 20:46 
AnswerRe: Can I use this in owner-drawn FIXED ComboBox? Pin
_Flaviu11-Aug-15 21:48
_Flaviu11-Aug-15 21:48 
GeneralRe: Can I use this in owner-drawn FIXED ComboBox? Pin
Itban Saeed Usmani11-Aug-15 22:53
professionalItban Saeed Usmani11-Aug-15 22:53 
GeneralRe: Can I use this in owner-drawn FIXED ComboBox? Pin
_Flaviu11-Aug-15 23:11
_Flaviu11-Aug-15 23:11 
GeneralRe: Can I use this in owner-drawn FIXED ComboBox? Pin
_Flaviu13-Aug-15 22:11
_Flaviu13-Aug-15 22:11 
QuestionON Win7 Compiled with VS2008 show no tooltips Pin
maplewang25-Mar-14 15:47
maplewang25-Mar-14 15:47 
AnswerRe: ON Win7 Compiled with VS2008 show no tooltips Pin
_Flaviu25-Mar-14 22:25
_Flaviu25-Mar-14 22:25 

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.