Click here to Skip to main content
Click here to Skip to main content

ListBox with Icons

, 8 Apr 2002
Rate this:
Please Sign up or sign in to vote.
Listbox Control with an image property for every item


All of us like more color or image in our control, so do I.

In this article, I give each item in the custom ListBox class an image property.

Note: my article has no source code because it very short and easy.

First: we create 2 classes for GListBox

// GListBoxItem class 
public class GListBoxItem
    private string _myText;
    private int _myImageIndex;
    // properties 
    public string Text
        get {return _myText;}
        set {_myText = value;}
    public int ImageIndex
        get {return _myImageIndex;}
        set {_myImageIndex = value;}
    public GListBoxItem(string text, int index)
        _myText = text;
        _myImageIndex = index;
    public GListBoxItem(string text): this(text,-1){}
    public GListBoxItem(): this(""){}
    public override string ToString()
        return _myText;
}//End of GListBoxItem class

// GListBox class 
public class GListBox : ListBox
    private ImageList _myImageList;
    public ImageList ImageList
        get {return _myImageList;}
        set {_myImageList = value;}
    public GListBox()
        // Set owner draw mode
        this.DrawMode = DrawMode.OwnerDrawFixed;
    protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
        GListBoxItem item;
        Rectangle bounds = e.Bounds;
        Size imageSize = _myImageList.ImageSize;
            item = (GListBoxItem) Items[e.Index];
            if (item.ImageIndex != -1)
                imageList.Draw(e.Graphics, bounds.Left,bounds.Top,item.ImageIndex); 
                e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor), 
                    bounds.Left+imageSize.Width, bounds.Top);
                e.Graphics.DrawString(item.Text, e.Font,new SolidBrush(e.ForeColor),
                    bounds.Left, bounds.Top);
            if (e.Index != -1)
                    new SolidBrush(e.ForeColor) ,bounds.Left, bounds.Top);
                e.Graphics.DrawString(Text,e.Font,new SolidBrush(e.ForeColor),
                    bounds.Left, bounds.Top);
}//End of GListBox class

After that, in order to use our code, we could do:

GListBox lb = new GListBox();
lb.ImageList = imageList;
lb.Items.Add( new GListBoxItem("Image 1",0));
lb.Items.Add( new GListBoxItem("Image 2",1));
lb.Items.Add( new GListBoxItem("Image 3",2));

That's all and thanks for reading.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Vietnam Vietnam
Nguyen Ha Giang,BS(computer science).

Comments and Discussions

GeneralMy vote of 3 PinmemberMarco Bertschi18-Feb-13 22:26 
GeneralMy vote of 1 Pinmembersurabimadu9-Oct-12 15:09 
QuestionVB.NET Code please Pinmembertharinda_8520-Oct-11 9:57 
GeneralMy vote of 1 Pinmembershabeerhameed4-Aug-11 5:07 
GeneralMy vote of 1 PinmemberChris C h a m b e r s11-Feb-10 5:48 
GeneralMy vote of 2 PingroupThomas Fredriksen2-Feb-10 7:16 
GeneralFixed some bugs, added HorizonalScrollbar and color functionality Pinmemberrockincpu24-Jan-10 5:29 
GeneralMy vote of 1 PinmemberPiyush Vardhan Singh23-Jul-09 21:15 
GeneralMy problem Pinmemberdrnox12-Jul-08 18:37 
GeneralCode Fixes Pinmemberdean.wilde27-Jun-08 14:02 
GeneralCode wrong Pinmemberbinit_rana23-Oct-07 8:32 
I think there are pieces of the code missing.
GeneralRemoving an item from GListBox PinmemberBrustem16-Oct-07 11:05 
GeneralRe: Removing an item from GListBox PinmemberPawel Gielmuda12-Nov-07 23:41 
GeneralHorizontal ScrollBar Pinmemberlaqula7-Mar-07 3:37 
GeneralClass in VB.NET... frist part Pinmemberdvdmena17-Jan-07 6:05 
Generalto add item to listBox Pinmemberteklay2-Dec-06 3:03 
GeneralTidied OnDrawItem Pinmembersatankidneypie3-May-06 5:35 
GeneralRe: Tidied OnDrawItem Pinmemberiammudman19-Dec-06 10:16 
GeneralRe: Tidied OnDrawItem Pinmemberfarang220-Feb-07 23:03 
GeneralRe: Tidied OnDrawItem PinmemberSystemExam25-Jul-09 9:24 
Questionwhy no full code? PinmemberDarchangel24-Sep-05 17:30 
GeneralFine Pinmemberbaskar.G20-Jul-05 21:43 
Generalusable version Pinmemberhoahong12-Dec-04 21:23 
QuestionWhat the heck is going on PinmemberApollo4x4-Mar-04 15:46 
QuestionSerious Design Flaw or Typo? PinmemberCode Deamon8-Oct-03 6:48 
GeneralListBox Que, please help PinsussXiaoTian1-May-03 17:47 
Generalview on my Form PinmemberHarireddyt22-Apr-03 11:46 
GeneralThanks a lot PinmemberNnamdi Onyeyiri4-Aug-02 6:18 
GeneralRe: text formatting PinmemberNish [BusterBoy]9-Apr-02 20:38 
GeneralRe: text formatting PinmemberNguyen Ha Giang9-Apr-02 22:04 
GeneralRe: text formatting PinmemberNish [BusterBoy]9-Apr-02 22:13 
GeneralRe: text formatting PinmemberAnonymous4-Jul-02 7:04 
GeneralRe: text formatting PinsussAnonymous14-Apr-03 23:30 
GeneralRe: text formatting PinmemberTranvanthang27-Oct-03 23:54 
GeneralRe: text formatting PinmemberTranvanthang28-Oct-03 0:03 

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
Web01 | 2.8.150326.1 | Last Updated 9 Apr 2002
Article Copyright 2002 by nhgiang
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid