Click here to Skip to main content
Licence 
First Posted 8 Apr 2002
Views 218,570
Bookmarked 46 times

ListBox with Icons

By | 8 Apr 2002 | Article
Listbox Control with an image property for every item

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_858:57 20 Oct '11  
GeneralMy vote of 1 Pinmembershabeerhameed4:07 4 Aug '11  
GeneralMy vote of 1 PinmemberChris C h a m b e r s4:48 11 Feb '10  
GeneralMy vote of 2 PingroupThomas Fredriksen6:16 2 Feb '10  
GeneralFixed some bugs, added HorizonalScrollbar and color functionality Pinmemberrockincpu4:29 24 Jan '10  
GeneralMy vote of 1 PinmemberPiyush Vardhan Singh20:15 23 Jul '09  
GeneralMy problem Pinmemberdrnox17:37 12 Jul '08  
GeneralCode Fixes Pinmemberdean.wilde13:02 27 Jun '08  
GeneralCode wrong Pinmemberbinit_rana7:32 23 Oct '07  
GeneralRemoving an item from GListBox PinmemberBrustem10:05 16 Oct '07  
GeneralRe: Removing an item from GListBox PinmemberPawel Gielmuda22:41 12 Nov '07  
GeneralHorizontal ScrollBar Pinmemberlaqula2:37 7 Mar '07  
GeneralClass in VB.NET... frist part Pinmemberdvdmena5:05 17 Jan '07  
I needed this very good class in VB. So i "translated" it.. But can you control if it good? Thanks
 
Public Class GListBoxItem
Private _myText As String
Private _myImageIndex As Int16
 

Public Property Text() As String
Get
Return _myText
End Get
Set(ByVal value As String)
_myText = value
End Set
End Property
 
Public Property ImageIndex() As Int16
Get
Return _myImageIndex
End Get
Set(ByVal value As Int16)
_myImageIndex = value
End Set
End Property
 
Public Sub New(ByVal Text As String, ByVal Index As Int16)
_myText = Text
_myImageIndex = Index
End Sub
 
Public Overrides Function ToString() As String
Return _myText
End Function
End Class
Generalto add item to listBox Pinmemberteklay2:03 2 Dec '06  
GeneralTidied OnDrawItem Pinmembersatankidneypie4:35 3 May '06  
GeneralRe: Tidied OnDrawItem Pinmemberiammudman9:16 19 Dec '06  
GeneralRe: Tidied OnDrawItem Pinmemberfarang222:03 20 Feb '07  
GeneralRe: Tidied OnDrawItem PinmemberSystemExam8:24 25 Jul '09  
Questionwhy no full code? PinmemberDarchangel16:30 24 Sep '05  
GeneralFine Pinmemberbaskar.G20:43 20 Jul '05  
Generalusable version Pinmemberhoahong20:23 12 Dec '04  
QuestionWhat the heck is going on PinmemberApollo4x14:46 4 Mar '04  
QuestionSerious Design Flaw or Typo? PinmemberCode Deamon5:48 8 Oct '03  
GeneralListBox Que, please help PinsussXiaoTian16:47 1 May '03  
Generalview on my Form PinmemberHarireddyt10: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
Web01 | 2.5.120529.1 | Last Updated 9 Apr 2002
Article Copyright 2002 by nhgiang
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid