Click here to Skip to main content
Licence 
First Posted 9 Jan 2004
Views 215,614
Bookmarked 103 times

Where's the ImageCombo control?

By | 18 Jan 2004 | Article
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

About the Author

Niels Penneman



United Kingdom United Kingdom

Member



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
GeneralMy vote of 5 PinmemberMoin Alvi1:10 26 Jul '11  
GeneralSupport for Datasource PinmemberBrian (BH)4:11 17 Jun '10  
GeneralLicense PinmemberMember 41792309:04 4 Apr '09  
GeneralRe: License PinmemberNiels Penneman12:01 4 Apr '09  
QuestionAuto Complete missing PinmemberMember 31856113:42 2 Jan '08  
GeneralIs somethingt liker this available for CheckedListBoxes Pinmembercht30003:34 5 Nov '07  
Hello,
 
I like to have an CheckedListBox which has a small image in addition to its text. The image would be different for each line, just like this Combo has. Does anybody know such a control?
Generalerror message at appending imag file to imagelist:doh: Pinmemberosaru22:28 24 Oct '07  
GeneralRe: error message at appending imag file to imagelist:doh: PinmemberNiels Penneman7:09 25 Oct '07  
GeneralAwesome! Pinmemberandrojdaz9:19 4 Oct '07  
QuestionAny plan to have Vista look for DropDownLists? PinmemberSteph17x5:42 28 Sep '07  
QuestionInvalidArgument=Value of '0' is not valid for 'index'. Pinmembershreekar20:16 7 Nov '06  
AnswerRe: InvalidArgument=Value of '0' is not valid for 'index'. Pinmembershreekar20:50 7 Nov '06  
GeneralRe: InvalidArgument=Value of '0' is not valid for 'index'. PinmemberNiels Penneman8:27 8 Nov '06  
QuestionRe: InvalidArgument=Value of '0' is not valid for 'index'. Pinmembershreekar17:49 8 Nov '06  
AnswerRe: InvalidArgument=Value of '0' is not valid for 'index'. PinmemberNiels Penneman9:47 9 Nov '06  
GeneralSmall suggestion PinmemberMattman2064:08 7 Nov '06  
GeneralBlurred images PinmemberColin Vella0:49 27 Oct '06  
GeneralThank you Pinmemberyeuue2:15 28 Jul '06  
GeneralGood jbo, PinmemberKony Han15:24 8 Mar '06  
GeneralOnPaint not fired PinmemberStefan Frutiger1:29 28 Nov '05  
GeneralRe: OnPaint not fired PinmemberHolo9119:15 26 Jan '06  
GeneralImageCombo not show icon Pinmembermicrolabs6:52 25 Oct '05  
QuestionBug with tabs? Pinmembermrgrieves23:56 6 Aug '05  
AnswerRe: Bug with tabs? Pinmembermrgrieves26:09 6 Aug '05  
GeneralRe: Bug with tabs? Pinmembermrgrieves27:40 14 Aug '05  

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