Click here to Skip to main content
Licence 
First Posted 8 Apr 2002
Views 213,925
Bookmarked 47 times

ListBox with Icons

By nhgiang | 8 Apr 2002
Listbox Control with an image property for every item
22 votes, 55.0%
1
4 votes, 10.0%
2
3 votes, 7.5%
3
4 votes, 10.0%
4
7 votes, 17.5%
5
2.04/5 - 48 votes
μ 2.37, σa 2.82 [?]

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionVB.NET Code please Pinmembertharinda_859:57 20 Oct '11  
GeneralMy vote of 1 Pinmembershabeerhameed5:07 4 Aug '11  
GeneralMy vote of 1 PinmemberChris C h a m b e r s5:48 11 Feb '10  
GeneralMy vote of 2 PingroupThomas Fredriksen7:16 2 Feb '10  
GeneralFixed some bugs, added HorizonalScrollbar and color functionality Pinmemberrockincpu5:29 24 Jan '10  
GeneralMy vote of 1 PinmemberPiyush Vardhan Singh21:15 23 Jul '09  
GeneralMy problem Pinmemberdrnox18:37 12 Jul '08  
GeneralCode Fixes Pinmemberdean.wilde14:02 27 Jun '08  
GeneralCode wrong Pinmemberbinit_rana8:32 23 Oct '07  
GeneralRemoving an item from GListBox PinmemberBrustem11:05 16 Oct '07  
GeneralRe: Removing an item from GListBox PinmemberPawel Gielmuda23:41 12 Nov '07  
GeneralHorizontal ScrollBar Pinmemberlaqula3:37 7 Mar '07  
GeneralClass in VB.NET... frist part Pinmemberdvdmena6:05 17 Jan '07  
Generalto add item to listBox Pinmemberteklay3:03 2 Dec '06  
GeneralTidied OnDrawItem Pinmembersatankidneypie5:35 3 May '06  
GeneralRe: Tidied OnDrawItem Pinmemberiammudman10:16 19 Dec '06  
GeneralRe: Tidied OnDrawItem Pinmemberfarang223:03 20 Feb '07  
GeneralRe: Tidied OnDrawItem PinmemberSystemExam9:24 25 Jul '09  
Questionwhy no full code? PinmemberDarchangel17:30 24 Sep '05  
GeneralFine Pinmemberbaskar.G21:43 20 Jul '05  
Generalusable version Pinmemberhoahong21:23 12 Dec '04  
QuestionWhat the heck is going on PinmemberApollo4x15:46 4 Mar '04  
QuestionSerious Design Flaw or Typo? PinmemberCode Deamon6:48 8 Oct '03  
GeneralListBox Que, please help PinsussXiaoTian17:47 1 May '03  
Generalview on my Form PinmemberHarireddyt11:46 22 Apr '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.

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