Click here to Skip to main content
11,427,908 members (57,895 online)
Click here to Skip to main content

Extended CComboBox

, 26 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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:

and combobox with list info tooltip over listbox items:

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:

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Flaviu2

Romania Romania
No Biography provided

Comments and Discussions

 
QuestionON Win7 Compiled with VS2008 show no tooltips Pin
maplewang25-Mar-14 16:47
membermaplewang25-Mar-14 16:47 
AnswerRe: ON Win7 Compiled with VS2008 show no tooltips Pin
Flaviu225-Mar-14 23:25
memberFlaviu225-Mar-14 23:25 
AnswerRe: ON Win7 Compiled with VS2008 show no tooltips Pin
Flaviu24-Dec-14 0:27
memberFlaviu24-Dec-14 0:27 
GeneralMy vote of 5 Pin
effort_fan23-Feb-14 1:36
membereffort_fan23-Feb-14 1:36 
QuestionSo Good Pin
lizhujiang26-Jan-14 5:16
grouplizhujiang26-Jan-14 5:16 
QuestionGOOD Pin
viveknet24-Jan-14 2:14
memberviveknet24-Jan-14 2:14 
AnswerRe: GOOD Pin
Flaviu224-Jan-14 3:09
memberFlaviu224-Jan-14 3:09 
GeneralGood Pin
Madhava Verma Dantuluri24-Jan-14 1:58
memberMadhava Verma Dantuluri24-Jan-14 1:58 
QuestionPrehistoric MFC Pin
EmoBemo10-Jun-13 6:05
memberEmoBemo10-Jun-13 6:05 
AnswerRe: Prehistoric MFC Pin
zlogdan11-Jun-13 8:57
professionalzlogdan11-Jun-13 8:57 
GeneralRe: Prehistoric MFC Pin
EmoBemo11-Jun-13 9:19
memberEmoBemo11-Jun-13 9:19 
GeneralRe: Prehistoric MFC Pin
zlogdan11-Jun-13 9:36
professionalzlogdan11-Jun-13 9:36 
GeneralRe: Prehistoric MFC Pin
YLAROYE24-Jan-14 4:09
memberYLAROYE24-Jan-14 4:09 
AnswerRe: Prehistoric MFC Pin
MeriInssi1-Feb-14 13:44
memberMeriInssi1-Feb-14 13:44 
AnswerRe: Prehistoric MFC Pin
RealSkydiver10-Nov-14 21:12
memberRealSkydiver10-Nov-14 21:12 
GeneralRe: Prehistoric MFC Pin
Flaviu210-Nov-14 21:16
memberFlaviu210-Nov-14 21:16 
GeneralRe: Prehistoric MFC Pin
EmoBemo11-Nov-14 0:26
memberEmoBemo11-Nov-14 0:26 
GeneralRe: Prehistoric MFC Pin
Flaviu211-Nov-14 21:29
memberFlaviu211-Nov-14 21:29 
GeneralRe: Prehistoric MFC Pin
EmoBemo12-Nov-14 0:35
memberEmoBemo12-Nov-14 0:35 
QuestionMouse cursor disappeared Pin
magicpapacy1-May-12 22:14
membermagicpapacy1-May-12 22:14 
AnswerRe: Mouse cursor disappeared Pin
Davide Zaccanti9-May-12 10:16
memberDavide Zaccanti9-May-12 10:16 
GeneralRe: Mouse cursor disappeared Pin
magicpapacy9-May-12 15:55
membermagicpapacy9-May-12 15:55 
Suggestion"Short and sweet" article is hard to understand Pin
DavidCrow18-Jul-11 9:58
memberDavidCrow18-Jul-11 9:58 
GeneralRe: "Short and sweet" article is hard to understand Pin
Flaviu218-Jul-11 20:35
memberFlaviu218-Jul-11 20:35 
QuestionA few function prototypes neeed to be changed to make your code compile under VS8 Pin
Dezhi Zhao13-Jul-11 18:20
memberDezhi Zhao13-Jul-11 18:20 
AnswerRe: A few function prototypes neeed to be changed to make your code compile under VS8 Pin
Flaviu214-Jul-11 3:39
memberFlaviu214-Jul-11 3:39 
GeneralRe: A few function prototypes neeed to be changed to make your code compile under VS8 Pin
Dezhi Zhao14-Jul-11 6:50
memberDezhi Zhao14-Jul-11 6:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 26 Mar 2014
Article Copyright 2011 by Flaviu2
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid