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

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
Nguyen Ha Giang,BS(computer science).

Comments and Discussions

 
GeneralMy vote of 3 PinmemberMarco Bertschi18-Feb-13 21:26 
GeneralMy vote of 1 Pinmembersurabimadu9-Oct-12 14:09 
QuestionVB.NET Code please Pinmembertharinda_8520-Oct-11 8:57 
GeneralMy vote of 1 Pinmembershabeerhameed4-Aug-11 4:07 
GeneralMy vote of 1 PinmemberChris C h a m b e r s11-Feb-10 4:48 
GeneralMy vote of 2 PingroupThomas Fredriksen2-Feb-10 6:16 
GeneralFixed some bugs, added HorizonalScrollbar and color functionality Pinmemberrockincpu24-Jan-10 4:29 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
 
namespace ImageListBox
{
    public partial class ImageListBox : ListBox
    {
        private int m_LastItemCount;
        private int m_LastClientWidth;
        private int m_LastImageSize;
        private int m_MaxStringHeigth;
        private ImageList m_ImageList;
 
        public ImageList ImageList
        {
            get { return m_ImageList; }
            set
            {
                m_ImageList = value;
            }
        }
 
        public ImageListBox()
        {
            InitializeComponent();
 
            //DrawMode = DrawMode.OwnerDrawFixed;
        }
 
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            ImageListBoxItem item = e.Index < 0 || this.DesignMode ? null : Items[e.Index] as ImageListBoxItem;
            bool draw = m_ImageList != null && (item != null);
 
            if (draw)
            {
                e.DrawBackground();
                e.DrawFocusRectangle();
 
                Size imageSize = m_ImageList.ImageSize;
                if (this.ItemHeight != imageSize.Height + 2) this.ItemHeight = imageSize.Height + 2;
 
                CheckHorizonalScroll(e.Graphics, e.Font);
 
                Rectangle bounds = e.Bounds;
                string strTextToDraw = item.ToString();
                Color color = e.ForeColor;
 
                if (item.ImageIndex > -1 && item.ImageIndex < m_ImageList.Images.Count)
                {
                    m_ImageList.Draw(e.Graphics, bounds.Left, bounds.Top, item.ImageIndex);
                    if (item.Color != Color.Empty) color = item.Color;
                }
 
                e.Graphics.DrawString(strTextToDraw, e.Font, new SolidBrush(color), /*bounds.Left*/0 + imageSize.Width, bounds.Top + (int)((this.ItemHeight - m_MaxStringHeigth) / 2));
            }
            else
            {
                base.OnDrawItem(e);
            }
        }
 
        private void CheckHorizonalScroll(Graphics g, Font f)
        {
            int maxStringWidth = 0;
            int maxStringHeight = 0;
            foreach (object item in Items)
            {
                string s = item.ToString();
                SizeF size = g.MeasureString(s, f);
                if (maxStringHeight < size.Height) maxStringHeight = (int)size.Height;
                if (maxStringWidth < size.Width) maxStringWidth = (int)size.Width;
            }
 
            m_LastImageSize = m_ImageList.ImageSize.Width;
            m_LastClientWidth = ClientSize.Width;
            m_LastItemCount = Items.Count;
            m_MaxStringHeigth = maxStringHeight;
 
            int he = maxStringWidth + m_LastImageSize;
            if (HorizontalExtent != he) HorizontalExtent = he;
 
        }
 
        public class ImageListBoxItem
        {
            private int m_ImageIndex;
            private object m_Item;
            private Color m_Color;
 
            public int ImageIndex
            {
                get { return m_ImageIndex; }
                set { m_ImageIndex = value; }
            }
 
            public object Item
            {
                get { return m_Item; }
                set { m_Item = value; }
            }
 
            public Color Color
            {
                get { return m_Color; }
                set { m_Color = value; }
            }
 

            public ImageListBoxItem() : this(null) { }
 
            public ImageListBoxItem(object item) : this(-1, item) { }
 
            public ImageListBoxItem(int imageIndex, object item) : this(imageIndex, Color.Empty, item) { }
 
            public ImageListBoxItem(int imageIndex, Color color, object item)
            {
                m_ImageIndex = imageIndex;
                m_Color = color;
                m_Item = item;
            }
 
            public override string ToString()
            {
                if (m_Item == null) return "Null";
                return m_Item.ToString();
            }
        }
    }
}

GeneralMy vote of 1 PinmemberPiyush Vardhan Singh23-Jul-09 20:15 
GeneralMy problem Pinmemberdrnox12-Jul-08 17:37 
GeneralCode Fixes Pinmemberdean.wilde27-Jun-08 13:02 
GeneralCode wrong Pinmemberbinit_rana23-Oct-07 7:32 
GeneralRemoving an item from GListBox PinmemberBrustem16-Oct-07 10:05 
GeneralRe: Removing an item from GListBox PinmemberPawel Gielmuda12-Nov-07 22:41 
GeneralHorizontal ScrollBar Pinmemberlaqula7-Mar-07 2:37 
GeneralClass in VB.NET... frist part Pinmemberdvdmena17-Jan-07 5:05 
Generalto add item to listBox Pinmemberteklay2-Dec-06 2:03 
GeneralTidied OnDrawItem Pinmembersatankidneypie3-May-06 4:35 
GeneralRe: Tidied OnDrawItem Pinmemberiammudman19-Dec-06 9:16 
GeneralRe: Tidied OnDrawItem Pinmemberfarang220-Feb-07 22:03 
GeneralRe: Tidied OnDrawItem PinmemberSystemExam25-Jul-09 8:24 
Questionwhy no full code? PinmemberDarchangel24-Sep-05 16:30 
GeneralFine Pinmemberbaskar.G20-Jul-05 20:43 
Generalusable version Pinmemberhoahong12-Dec-04 20:23 
QuestionWhat the heck is going on PinmemberApollo4x4-Mar-04 14:46 
QuestionSerious Design Flaw or Typo? PinmemberCode Deamon8-Oct-03 5:48 

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 | Mobile
Web01 | 2.8.140709.1 | Last Updated 9 Apr 2002
Article Copyright 2002 by nhgiang
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid