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

ListBox with Icons

By , 8 Apr 2002
 

Introduction

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;}
    }
    //constructor
    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)
    {
        e.DrawBackground();
        e.DrawFocusRectangle();
        GListBoxItem item;
        Rectangle bounds = e.Bounds;
        Size imageSize = _myImageList.ImageSize;
        try
        {
            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);
            }
            else
            {
                e.Graphics.DrawString(item.Text, e.Font,new SolidBrush(e.ForeColor),
                    bounds.Left, bounds.Top);
            }
        }
        catch
        {
            if (e.Index != -1)
            {
                e.Graphics.DrawString(Items[e.Index].ToString(),e.Font, 
                    new SolidBrush(e.ForeColor) ,bounds.Left, bounds.Top);
            }
            else
            {
                e.Graphics.DrawString(Text,e.Font,new SolidBrush(e.ForeColor),
                    bounds.Left, bounds.Top);
            }
        }
        base.OnDrawItem(e);
    }
}//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.

License

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

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 3memberMarco Bertschi18 Feb '13 - 21:26 
GeneralMy vote of 1membersurabimadu9 Oct '12 - 14:09 
QuestionVB.NET Code pleasemembertharinda_8520 Oct '11 - 8:57 
GeneralMy vote of 1membershabeerhameed4 Aug '11 - 4:07 
GeneralMy vote of 1memberChris C h a m b e r s11 Feb '10 - 4:48 
GeneralMy vote of 2groupThomas Fredriksen2 Feb '10 - 6:16 
GeneralFixed some bugs, added HorizonalScrollbar and color functionalitymemberrockincpu24 Jan '10 - 4:29 
GeneralMy vote of 1memberPiyush Vardhan Singh23 Jul '09 - 20:15 
GeneralMy problemmemberdrnox12 Jul '08 - 17:37 
GeneralCode Fixesmemberdean.wilde27 Jun '08 - 13:02 
GeneralCode wrongmemberbinit_rana23 Oct '07 - 7:32 
GeneralRemoving an item from GListBoxmemberBrustem16 Oct '07 - 10:05 
GeneralRe: Removing an item from GListBoxmemberPawel Gielmuda12 Nov '07 - 22:41 
GeneralHorizontal ScrollBarmemberlaqula7 Mar '07 - 2:37 
GeneralClass in VB.NET... frist partmemberdvdmena17 Jan '07 - 5:05 
Generalto add item to listBoxmemberteklay2 Dec '06 - 2:03 
GeneralTidied OnDrawItemmembersatankidneypie3 May '06 - 4:35 
GeneralRe: Tidied OnDrawItemmemberiammudman19 Dec '06 - 9:16 
GeneralRe: Tidied OnDrawItemmemberfarang220 Feb '07 - 22:03 
GeneralRe: Tidied OnDrawItemmemberSystemExam25 Jul '09 - 8:24 
Questionwhy no full code?memberDarchangel24 Sep '05 - 16:30 
GeneralFinememberbaskar.G20 Jul '05 - 20:43 
Generalusable versionmemberhoahong12 Dec '04 - 20:23 
QuestionWhat the heck is going onmemberApollo4x4 Mar '04 - 14:46 
QuestionSerious Design Flaw or Typo?memberCode Deamon8 Oct '03 - 5:48 
GeneralListBox Que, please helpsussXiaoTian1 May '03 - 16:47 
Generalview on my FormmemberHarireddyt22 Apr '03 - 10:46 
GeneralThanks a lotmemberNnamdi Onyeyiri4 Aug '02 - 5:18 
GeneralRe: text formattingmemberNish [BusterBoy]9 Apr '02 - 19:38 
GeneralRe: text formattingmemberNguyen Ha Giang9 Apr '02 - 21:04 
GeneralRe: text formattingmemberNish [BusterBoy]9 Apr '02 - 21:13 
GeneralRe: text formattingmemberAnonymous4 Jul '02 - 6:04 
GeneralRe: text formattingsussAnonymous14 Apr '03 - 22:30 
GeneralRe: text formattingmemberTranvanthang27 Oct '03 - 22:54 
GeneralRe: text formattingmemberTranvanthang27 Oct '03 - 23:03 

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

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