Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

List ComboBox Control

4.98/5 (15 votes)
23 Sep 2016CPOL2 min read 24.6K   1.2K  
A ListCtrl ComboBox control

Introduction

This control enhances the classic CComboBox control, and puts in its dropdown list a CListCtrl, with all benefits that come after: drop down list could have multicolumn, checkboxes, ordering, etc.

Background

This control is related with this one: CTreeComboBox, the difference consists only in control that is encapsulated in the dropdown list.

Using the Code

In order to be used, this control must have 3 classes:

  • CListComboBox, derived from CComboBox
  • CComboListCtrl, derived from CListCtrl
  • CYourComboListCtrl, derived from CComboListCtrl

Description

The CYourComboListCtrl is only a customization of your CListCtrl that you want to use, but it must be derived from CComboListCtrl in order to have the applied functionality.

So, if you want this control hybrid, you just have to include 6 files, ListComboBox.h and cpp, ComboListCtrl.h and cpp, and an extension of CComboListCtrl, let's say MyComboListCtrl.h and cpp.

Once you have these files in your project, you can easily use this control just like this:

Put a CComboBox control on your form, but it must be type of CListComboBox, not CComoboBox only.

C#
//
// In your view header

#include "ListComboBox.h"
#include "MyComboListCtrl.h"

// and

 CListComboBox m_Combo2;
 CMyComboListCtrl* m_pListCtrl2;

// in your cpp file

CTestListComboView::CTestListComboView()
 : CFormView(CTestListComboView::IDD)
 ,m_pListCtrl2(NULL)
{
 m_pListCtrl2 = new CMyComboListCtrl;
 m_Combo2.SetListCtrl(m_pListCtrl2);
}

And after that, you just handle your m_pListCtrl2 as you want (configurating, populate with data, etc.)

You cand find more details in the attached sample project.

The enhanced combobox has several methods that help in using this hybrid control:

  • DisplayList() - Use this instead of native ShowDopDown() in order to show drop down menu
  • SetEditText(SetEditText(LPCTSTR lpszString) - To setup a text for edit combobox
  • SetEditItemData/GetEditItemData - To setup an itemdata for selected item, visible from CListComboBox object
  • SetListCtrl(CComboListCtrl* pListCtrl) - is setting the CComboListCtrl object as drop down list
  • IsControlActive() - Retrieve the state of drop down list
  • GetDroppedWidth() / GetDroppedHeight() - Return the width and height of drop down list
  • SetDroppedWidth() / SetDroppedHeight() - Setup the width and height of drop down list
  • SetEditTooltip() / GetEditTooltip() - Set/get the tooltip for edit combobox, that could be different than edit text
  • GetShowTooltip() / SetShowTooltip() - Activate / deactivate the tooltip for edit combobox
  • GetShowEditTooltipOverItem() / SetShowEditTooltipOverItem() - Set / Get the position of tooltip for edit combobox, to be shown over edit area, of above

Of course, there is another method available on this CComboBox that makes from this control a good choice for situations when you need an enhanced combobox.

I hope it helps you! P.S.

As far as I can, I will come back with new details about using this control.

History

  • 2016-09-23- Article published

License

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