CIconListBox - A list box with Icons

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


Comments

  • Set-/GetItemData() needs to be overwritten

    Posted by Legacy on 01/30/2003 12:00am

    Originally posted by: Kurt Santel�

    This "tuned" listbox works great, except if you have already used the function SetItemData() for another purpose. Catching this function, and overwriting it would solve the problem. Or another way of storing which icon needs to be displayed is another solution.

    greetz
    Kurt

    Reply
  • Thanks Sekhar

    Posted by Legacy on 11/11/2002 12:00am

    Originally posted by: Mash

    Was looking for implementing icons in the listbox.
    This serves my purpose perfect.

    Reply
  • Is it possible to use it in WinCE?

    Posted by Legacy on 08/05/2002 12:00am

    Originally posted by: Peter

    Hi. I'm trying to use the class in application written in eMbedded Visual C++. Will it work? If it will, what should I add and where?
    Thanks in advance

    Reply
  • how to do it in sdk

    Posted by Legacy on 02/17/2002 12:00am

    Originally posted by: deepak

    It's good in MFC.
    But if I want to implement it in SDK?
    How to do it?

    Reply
  • Problem I used it but my first item in listbox does not display the right color?

    Posted by Legacy on 02/01/2002 12:00am

    Originally posted by: Mahshid

    Hi, Thanks for the code. i followed the instruction in this page to add the Listbox, My first item in the list always display my third item in the list and it is not correct. Does anybody know what my mistake is?
    Thanks

    Reply
  • Nice! One Change...

    Posted by Legacy on 10/16/2001 12:00am

    Originally posted by: Chris Adamson

    In the method:
    
    

    void CIconListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)

    << snip >>
    I changed the code so that it draws the text next to the various sized bitmaps (instead of hard coding 16 pixels).
    << snip >>

    if(m_pImageList)
    {
    IMAGEINFO inf;
    VERIFY(m_pImageList->GetImageInfo(0, &inf));
    rClient.left += inf.rcImage.right;
    rText.left += inf.rcImage.right + 2;
    rText.top += 2;
    }

    Other than that, thanks for the class!
    Chris Adamson

    Reply
  • Ex(Properties)List

    Posted by Legacy on 10/07/2001 12:00am

    Originally posted by: Mike Philis

    Do you need an advanced list control? Check this out:
    
    http://www.exontrol.com/sg.jsp?content=products/exlist

    Mike
    www.exontrol.com

    Reply
  • MultiSelect -> SelItemRange -> Disaster!!

    Posted by Legacy on 09/20/2001 12:00am

    Originally posted by: Raj

    Hi
    Excellent work!!
    I'd like to point out a problem with your Iconic listbox.
    1. List box style is made "Multiple" or "Extended"
    2. Add a check box or any other toggle control to your dialog.
    3. In the BN_CLICKED event of the above control, add the following code:
    void CIconLBDemoDlg::OnCheck1()
    {
    int state = ((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck();
    CListBox* pListBox = (CListBox*)GetDlgItem(IDC_ICONLISTBOX);
    pListBox->SelItemRange(state, 0, 5);
    }

    The bk color of the list box remains blue.. when we use SelItemRange.

    Thankx

    Reply
  • A free tool to rename an Existing Visual Studio project.

    Posted by Legacy on 09/07/2001 12:00am

    Originally posted by: Steven

    UCanCode Just release a new version of toolbox,you can use it to rename an existing visual studio project or to backup an existing visual studio project.download a free copy by visit http://www.ucancode.net

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Is your sales and operations planning helping or hurting your bottom line? Here are 5 useful tips from the experts at Quintiq to guide you to a better S&OP strategy.

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds