CIconListBox - A list box with Icons | CodeGuru

CIconListBox – A list box with Icons

Environment: Windows NT4 SP4, Visual C++ 6 I needed a listbox showing Icons and text strings in our Application. When I didn’t find any, I decided to create one. This List Box supports changing Icon for an item and removing the ImageList for the List Box at runtime. How To Use It To use this […]

Written By
CodeGuru Staff
CodeGuru Staff
Sep 4, 2001
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: Windows NT4 SP4, Visual C++ 6

IconListBox

I needed a listbox showing Icons and text strings in our Application. When I didn’t find any, I decided to create one. This List Box supports changing Icon for an item and removing the ImageList for the List Box at runtime.

How To Use It

To use this listbox class, simply drag a list box from the controls palette and drop it in the dialog Box. Set the properties Owner Draw style to Fixed or Variable, and with the Has Strings style checked.

Following are the steps to add a CIconListBox Class to a dialog:

  • Add IconListBox.cpp and IconListBox.h to your project.
  • Add include "IconListBox.h" to the
    header file of your dialog class.
  • Use ClassWizard to add a member variable of
    control type for the List Box. Then replace the CListbox type
    with CIconListBox.
// in dialog.h
//{{AFX_DATA(CIconLBDemoDlg)
enum { IDD = IDD_ICONLBDEMO_DIALOG };
CIconListBox	m_ListBox;
//}}AFX_DATA

To set the ImageList, item text and/or Image, the overloaded public methods of CIconListBox Class are used:

int AddString(LPCTSTR lpszItem);
int AddString(LPCTSTR lpszItem, int iImg);
int InsertString(int iIndex, LPCTSTR lpszItem);
int InsertString(int iIndex, LPCTSTR lpszItem, int iImg);
void SetItemImage(int iIndex, int iImg);
inline void SetImageList(CImageList* pImgList = NULL)
{ m_pImageList = pImgList; }

In the demo application the Image list is set to List Box in the dialog’s OnInitDialog() function. InitImageLists() method is the Dialog class function which is used to create the Image List.

// TODO: Add extra initialization here
InitImageLists();
m_ListBox.SetImageList(&m_imgNormal);

Downloads

Download demo project – 24 Kb

Download source – 3 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.