Click here to Skip to main content
13,904,914 members
Click here to Skip to main content
Add your own
alternative version


48 bookmarked
Posted 8 Apr 2002

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).

You may also be interested in...

Comments and Discussions

QuestionSerious Design Flaw or Typo? Pin
Code Deamon8-Oct-03 5:48
memberCode Deamon8-Oct-03 5:48 
GeneralListBox Que, please help Pin
Huang, XiaoTian1-May-03 16:47
memberHuang, XiaoTian1-May-03 16:47 
Generalview on my Form Pin
Harireddyt22-Apr-03 10:46
memberHarireddyt22-Apr-03 10:46 
GeneralThanks a lot Pin
Nnamdi Onyeyiri4-Aug-02 5:18
memberNnamdi Onyeyiri4-Aug-02 5:18 
GeneralRe: text formatting Pin
Nish Nishant9-Apr-02 19:38
sitebuilderNish Nishant9-Apr-02 19:38 
GeneralRe: text formatting Pin
nhgiang9-Apr-02 21:04
membernhgiang9-Apr-02 21:04 
GeneralRe: text formatting Pin
Nish Nishant9-Apr-02 21:13
sitebuilderNish Nishant9-Apr-02 21:13 
GeneralRe: text formatting Pin
4-Jul-02 6:04
suss4-Jul-02 6:04 
GeneralRe: text formatting Pin
Anonymous14-Apr-03 22:30
memberAnonymous14-Apr-03 22:30 
GeneralRe: text formatting Pin
Tranvanthang27-Oct-03 22:54
memberTranvanthang27-Oct-03 22:54 
GeneralRe: text formatting Pin
Tranvanthang27-Oct-03 23:03
memberTranvanthang27-Oct-03 23:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190306.1 | Last Updated 9 Apr 2002
Article Copyright 2002 by nhgiang
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid