Click here to Skip to main content
11,644,982 members (67,564 online)
Click here to Skip to main content

Where's the ImageCombo control?

, 18 Jan 2004 236.5K 4.9K 102
Rate this:
Please Sign up or sign in to vote.
ImageCombo control for .NET.

Sample Image - ImageCombo.NET

Introduction

When I started programming in .NET, I wondered where the ImageCombo control had gone (I used to program in VB6). The .NET Framework didn't seem to have this functionality built-in, but it's very easy to add it.

Using the code

The code consists of two classes: ImageCombo and ImageComboItem. If you want to use them in C#, just copy them into your project; for other .NET languages, you can add a reference to the library.

The control inherits from ComboBox and introduces one new member: the ImageList property, which doesn't require any further explanation. The control is owner drawn and there is a custom drawing method defined, so don't change its DrawMode property if you want to see the images.

The ImageComboItem class inherits from Object. You can set a customForeColor and it has a Mark property which determines if the item is shown in bold font style (does not work if owner font is already bold).

To add an item to an ImageCombo with text "Icon 0" and image index 0, use the following code:

imageCombo.Items.Add(new ImageComboItem("Icon 0", 0));

Code listing: ImageCombo class

using System;
using System.Drawing;

namespace System.Windows.Forms
{

    public class ImageCombo : ComboBox
    {
        private ImageList imgs = new ImageList();

        // constructor
        public ImageCombo()
        {
            // set draw mode to owner draw
            this.DrawMode = DrawMode.OwnerDrawFixed;    
        }

        // ImageList property
        public ImageList ImageList 
        {
            get 
            {
                return imgs;
            }
            set 
            {
                imgs = value;
            }
        }

        // customized drawing process
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            // draw background & focus rect
            e.DrawBackground();
            e.DrawFocusRectangle();

            // check if it is an item from the Items collection
            if (e.Index < 0)

                // not an item, draw the text (indented)
                e.Graphics.DrawString(this.Text, e.Font, 
                        new SolidBrush(e.ForeColor), e.Bounds.Left + 
                        imgs.ImageSize.Width, e.Bounds.Top);

            else
            {
                
                // check if item is an ImageComboItem
                if (this.Items[e.Index].GetType() == typeof(ImageComboItem)) 
                {                                                            

                    // get item to draw
                    ImageComboItem item = (ImageComboItem) 
                        this.Items[e.Index];

                    // get forecolor & font
                    Color forecolor = (item.ForeColor != 
                         Color.FromKnownColor(KnownColor.Transparent)) ? 
                         item.ForeColor : e.ForeColor;
                    Font font = item.Mark ? new Font(e.Font, 
                         FontStyle.Bold) : e.Font;

                    // -1: no image
                    if (item.ImageIndex != -1) 
                    {
                        // draw image, then draw text next to it
                        this.ImageList.Draw(e.Graphics, 
                           e.Bounds.Left, e.Bounds.Top, item.ImageIndex);
                        e.Graphics.DrawString(item.Text, font, 
                           new SolidBrush(forecolor), e.Bounds.Left + 
                           imgs.ImageSize.Width, e.Bounds.Top);
                    }
                    else
                        // draw text (indented)
                        e.Graphics.DrawString(item.Text, font, 
                            new SolidBrush(forecolor), e.Bounds.Left + 
                            imgs.ImageSize.Width, e.Bounds.Top);

                }
                else
                
                    // it is not an ImageComboItem, draw it
                    e.Graphics.DrawString(this.Items[e.Index].ToString(), 
                      e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + 
                      imgs.ImageSize.Width, e.Bounds.Top);
                
            }

            base.OnDrawItem (e);
        }
        
    }

}

Code listing: ImageComboItem class

using System;
using System.Drawing;

namespace System.Windows.Forms
{

    public class ImageComboItem : object
    {
        // forecolor: transparent = inherit
        private Color forecolor = Color.FromKnownColor(
                KnownColor.Transparent);
        private bool mark = false;
        private int imageindex = -1;
        private object tag = null;
        private string text = null;        
        
        // constructors
        public ImageComboItem()
        {
        }

        public ImageComboItem(string Text) 
        {
            text = Text;    
        }

        public ImageComboItem(string Text, int ImageIndex)
        {
            text = Text;
            imageindex = ImageIndex;
        }

        public ImageComboItem(string Text, int ImageIndex, bool Mark)
        {
            text = Text;
            imageindex = ImageIndex;
            mark = Mark;
        }

        public ImageComboItem(string Text, int ImageIndex, 
            bool Mark, Color ForeColor)
        {
            text = Text;
            imageindex = ImageIndex;
            mark = Mark;
            forecolor = ForeColor;
        }

        public ImageComboItem(string Text, int ImageIndex, 
               bool Mark, Color ForeColor, object Tag)
        {
            text = Text;
            imageindex = ImageIndex;
            mark = Mark;
            forecolor = ForeColor;
            tag = Tag;
        }

        // forecolor
        public Color ForeColor 
        {
            get 
            {
                return forecolor;
            }
            set
            {
                forecolor = value;
            }
        }

        // image index
        public int ImageIndex 
        {
            get 
            {
                return imageindex;
            }
            set 
            {
                imageindex = value;
            }
        }

        // mark (bold)
        public bool Mark
        {
            get
            {
                return mark;
            }
            set
            {
                mark = value;
            }
        }

        // tag
        public object Tag
        {
            get
            {
                return tag;
            }
            set
            {
                tag = value;
            }
        }

        // item text
        public string Text 
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
            }
        }
        
        // ToString() should return item text
        public override string ToString() 
        {
            return text;
        }

    }

}

History

  • Original code (01/10/2004)
  • Optimized code for speed & added some functionality (18/10/2004)

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

Share

About the Author

Niels Penneman
Belgium Belgium
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Moin Alvi26-Jul-11 1:10
memberMoin Alvi26-Jul-11 1:10 
GeneralSupport for Datasource Pin
Brian (BH)17-Jun-10 4:11
memberBrian (BH)17-Jun-10 4:11 
GeneralLicense Pin
Member 41792304-Apr-09 9:04
memberMember 41792304-Apr-09 9:04 
GeneralRe: License Pin
Niels Penneman4-Apr-09 12:01
memberNiels Penneman4-Apr-09 12:01 
QuestionAuto Complete missing Pin
Member 31856112-Jan-08 3:42
memberMember 31856112-Jan-08 3:42 
GeneralIs somethingt liker this available for CheckedListBoxes Pin
cht30005-Nov-07 3:34
membercht30005-Nov-07 3:34 
Generalerror message at appending imag file to imagelist:doh: Pin
osaru24-Oct-07 22:28
memberosaru24-Oct-07 22:28 
GeneralRe: error message at appending imag file to imagelist:doh: Pin
Niels Penneman25-Oct-07 7:09
memberNiels Penneman25-Oct-07 7:09 
GeneralAwesome! Pin
androjdaz4-Oct-07 9:19
memberandrojdaz4-Oct-07 9:19 
QuestionAny plan to have Vista look for DropDownLists? Pin
Steph17x28-Sep-07 5:42
memberSteph17x28-Sep-07 5:42 
QuestionInvalidArgument=Value of '0' is not valid for 'index'. Pin
shreekar7-Nov-06 20:16
membershreekar7-Nov-06 20:16 
AnswerRe: InvalidArgument=Value of '0' is not valid for 'index'. Pin
shreekar7-Nov-06 20:50
membershreekar7-Nov-06 20:50 
GeneralRe: InvalidArgument=Value of '0' is not valid for 'index'. Pin
Niels Penneman8-Nov-06 8:27
memberNiels Penneman8-Nov-06 8:27 
QuestionRe: InvalidArgument=Value of '0' is not valid for 'index'. Pin
shreekar8-Nov-06 17:49
membershreekar8-Nov-06 17:49 
AnswerRe: InvalidArgument=Value of '0' is not valid for 'index'. Pin
Niels Penneman9-Nov-06 9:47
memberNiels Penneman9-Nov-06 9:47 
GeneralSmall suggestion Pin
Mattman2067-Nov-06 4:08
memberMattman2067-Nov-06 4:08 
GeneralBlurred images Pin
Colin Vella27-Oct-06 0:49
memberColin Vella27-Oct-06 0:49 
GeneralThank you Pin
yeuue28-Jul-06 2:15
memberyeuue28-Jul-06 2:15 
GeneralGood jbo, Pin
Kony Han8-Mar-06 15:24
memberKony Han8-Mar-06 15:24 
GeneralOnPaint not fired Pin
Stefan Frutiger28-Nov-05 1:29
memberStefan Frutiger28-Nov-05 1:29 
GeneralRe: OnPaint not fired Pin
Holo91126-Jan-06 9:15
memberHolo91126-Jan-06 9:15 
GeneralImageCombo not show icon Pin
microlabs25-Oct-05 6:52
membermicrolabs25-Oct-05 6:52 
QuestionBug with tabs? Pin
mrgrieves26-Aug-05 3:56
membermrgrieves26-Aug-05 3:56 
AnswerRe: Bug with tabs? Pin
mrgrieves26-Aug-05 6:09
membermrgrieves26-Aug-05 6:09 
GeneralRe: Bug with tabs? Pin
mrgrieves214-Aug-05 7:40
membermrgrieves214-Aug-05 7:40 
GeneralHi, Niels Pin
iro197725-Jul-05 23:02
memberiro197725-Jul-05 23:02 
GeneralRe: Hi, Niels Pin
Niels Penneman26-Jul-05 5:36
memberNiels Penneman26-Jul-05 5:36 
GeneralNow tried 2 articles here which dosn't work. Pin
maunleif13-Dec-04 15:05
membermaunleif13-Dec-04 15:05 
GeneralRe: Now tried 2 articles here which dosn't work. Pin
Niels Penneman28-Feb-05 6:40
memberNiels Penneman28-Feb-05 6:40 
GeneralRe: Now tried 2 articles here which dosn't work. Pin
Robert Prouse23-Mar-05 3:57
memberRobert Prouse23-Mar-05 3:57 
GeneralRe: Now tried 2 articles here which dosn't work. Pin
Anonymous31-Jul-05 13:56
sussAnonymous31-Jul-05 13:56 
QuestionHow can i get the tag ? Pin
vbnetuk3-Nov-04 8:09
membervbnetuk3-Nov-04 8:09 
AnswerRe: How can i get the tag ? Pin
bondu_raja3-Nov-04 9:23
memberbondu_raja3-Nov-04 9:23 
GeneralRe: How can i get the tag ? Pin
vbnetuk3-Nov-04 21:55
membervbnetuk3-Nov-04 21:55 
GeneralRe: How can i get the tag ? Pin
vbnetuk3-Nov-04 21:57
membervbnetuk3-Nov-04 21:57 
GeneralVB.NET Version Pin
bondu_raja1-Nov-04 9:43
memberbondu_raja1-Nov-04 9:43 
GeneralRe: VB.NET Version Pin
vbnetuk3-Nov-04 7:57
membervbnetuk3-Nov-04 7:57 
QuestionRe: VB.NET Version Pin
shreekar7-Nov-06 20:18
membershreekar7-Nov-06 20:18 
GeneralSeparator Bar between ImageComboItems Pin
steven york9-Sep-04 15:04
susssteven york9-Sep-04 15:04 
GeneralRe: Separator Bar between ImageComboItems Pin
Niels Penneman28-Feb-05 6:42
memberNiels Penneman28-Feb-05 6:42 
GeneralYou are Good Pin
H_LionHeart11-Jul-04 13:18
memberH_LionHeart11-Jul-04 13:18 
GeneralLoading image at runtime Pin
jameschoa8129-May-04 4:28
memberjameschoa8129-May-04 4:28 
Generalgood work Pin
WillemM22-May-04 9:19
memberWillemM22-May-04 9:19 
GeneralSize of icon Pin
agentbb0076-May-04 11:14
memberagentbb0076-May-04 11:14 
GeneralRe: Size of icon Pin
Niels Penneman16-May-04 2:13
memberNiels Penneman16-May-04 2:13 
GeneralASP.NET version Pin
Mikhail_Sh5-Mar-04 4:54
memberMikhail_Sh5-Mar-04 4:54 
Questionwhat about a vb.net version Pin
vbnetuk4-Mar-04 22:48
membervbnetuk4-Mar-04 22:48 
AnswerRe: what about a vb.net version Pin
Niels Penneman5-Mar-04 1:02
memberNiels Penneman5-Mar-04 1:02 
AnswerRe: what about a vb.net version Pin
Michael Hutchinson14-May-04 4:20
memberMichael Hutchinson14-May-04 4:20 
GeneralProblem Pin
insipid1412-Feb-04 3:40
memberinsipid1412-Feb-04 3:40 

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 | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 19 Jan 2004
Article Copyright 2004 by Niels Penneman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid