CIconListBox – A list box with Icons

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

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read