Click here to Skip to main content
11,478,562 members (66,412 online)
Click here to Skip to main content

Implementing an OwnerDrawn ComboBox

, 3 Feb 2002 261.1K 2.4K 56
Rate this:
Please Sign up or sign in to vote.
This article shows how to create an OwnerDrawn combobox
<!-- Article image -->

Sample Image - OwnerDrawnComboBox.gif

<!-- Add the rest of your HTML here -->

Introduction

Hi there, I've been waiting for ages and ages for someone to kindly post the code for an owner drawn ComboBox but alas, to no avail. That's right, I'm your average, lowly leech and I've been stealing code off CodeProject for a long time now. Well looks like this time I actually had to go write something on my own. My guess is it was too simple a problem for most of you to bother with. But please do keep writing articles for relative beginners because most of us have to start with the simple things first. Luckily, writing the code turned out to be childs play with the .NET framework.

Ok, lets begin. First thing to do is drop a ComboBox onto a form and change it's DrawMode property to OwnerDrawFixed. This will mean that all the items appearing in the ComboBox will have the same dimensons. If you're going to draw items in the ComboBox that are of variable sizes then you would change the property to OwnerDrawVariable and be forced to deal with a MeasureItem event. I won't be covering that here. Next, create a DataSource for the ComboBox, as well as a corresponding collection of images. In my case I simply used two Arrays The following code should now be easy enough to follow

The Code

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication7
{    
    public class Form1 : System.Windows.Forms.Form
    {        
        private System.ComponentModel.Container components = null;
        private String[] arr;
        private Image[] imageArr;
        private System.Windows.Forms.ComboBox comboBox1;        
        public Font myFont;        
        
        public Form1()
        {            
            InitializeComponent();
            myFont = new System.Drawing.Font("Comic Sans", 11);
            
            arr = new String[4];
            arr[0] = "Smiley Red Face";
            arr[1] = "Smiley Cry";
            arr[2] = "Smiley Big Grin";
            arr[3]  = "Smiley Suss";
            
            this.comboBox1.DataSource = arr; //set the combo's data source to out array

            imageArr = new Image[4];

            /* I stole these images off CP. I specifically chose these images because they 
            are all little 16x16 sized gifs. If you're going to use your own images make 
            sure they are all the same size, otherwise your code won't work as advertised. 
            In that case you will have to set the DrawMode property of your combobox to 
            OwnnerDrawVariable and catch the MeasureItem event*/

            imageArr[0] = new Bitmap("C:\\smiley_redface.gif");
            imageArr[1] = new Bitmap("C:\\smiley_cry.gif");
            imageArr[2] = new Bitmap("C:\\smiley_biggrin.gif");
            imageArr[3] = new Bitmap("C:\\smiley_suss.gif");
        }        
        
        //You have your windows forms designer generated code here

        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void comboBox1_DrawItem(object sender, 
                                        System.Windows.Forms.DrawItemEventArgs e)
        {    
            // Let's highlight the currently selected item like any well 
            // behaved combo box should
            e.Graphics.FillRectangle(Brushes.Bisque, e.Bounds);                
            e.Graphics.DrawString(arr[e.Index], myFont, Brushes.Blue, 
                                  new Point(imageArr[e.Index].Width*2,e.Bounds.Y));       
            e.Graphics.DrawImage(imageArr[e.Index], new Point(e.Bounds.X, e.Bounds.Y));   
            
            //is the mouse hovering over a combobox item??            
            if((e.State & DrawItemState.Focus)==0)
            {                                                    
                //this code keeps the last item drawn from having a Bisque background. 
                e.Graphics.FillRectangle(Brushes.White, e.Bounds);                    
                e.Graphics.DrawString(arr[e.Index], myFont, Brushes.Blue, 
                                      new Point(imageArr[e.Index].Width*2,e.Bounds.Y));
                e.Graphics.DrawImage(imageArr[e.Index], new Point(e.Bounds.X, e.Bounds.Y)); 
            }    
        }                                                                
    }
}

Conclusion

Ok, that's it, my very first CodeProject article, go easy on me. The code started out quite long and tedious but I whittled it down as my understanding of DrawItemEventArgs became a bit clearer. I'm not too sure if it's cool to have the ComboBox default backcolor to be anything other than white. But that seems to be more a matter of taste.

Regards, Senkwe

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

Senkwe Chanda
Web Developer
South Africa South Africa
Nada

Comments and Discussions

 
GeneralMy vote of 5 Pin
manoj kumar choubey20-Feb-12 2:49
membermanoj kumar choubey20-Feb-12 2:49 
QuestionHow do I implement and host an owner drawn combobox inside DataGridView cells in C# Pin
Rodzaidi12-May-08 0:25
memberRodzaidi12-May-08 0:25 
QuestionAny plan to have Vista look for DropDownLists? Pin
Steph17x28-Sep-07 6:32
memberSteph17x28-Sep-07 6:32 
GeneralUse this technique to display enumerations with spaces Pin
Boinst16-Apr-07 0:19
memberBoinst16-Apr-07 0:19 
NewsAdvanced list box and combo box Pin
brett5630-Nov-05 6:24
memberbrett5630-Nov-05 6:24 
GeneralColors in Combo Box Pin
yahya_1322-Jan-05 23:22
memberyahya_1322-Jan-05 23:22 
GeneralRe: Colors in Combo Box Pin
LongHC6-May-05 10:26
memberLongHC6-May-05 10:26 
In the DrawItem event e.backGround : change it.
GeneralChanges to have a kind of simple ComboTree Pin
Vejas7-Jun-04 0:00
memberVejas7-Jun-04 0:00 
GeneralShow ComboBox in Web Application Form Pin
selcanberber9-Jan-04 1:56
sussselcanberber9-Jan-04 1:56 
QuestionHow to write a check combo Pin
xalee15-Dec-03 14:45
memberxalee15-Dec-03 14:45 
AnswerRe: How to write a check combo Pin
b.hartmann20-Jan-04 21:47
memberb.hartmann20-Jan-04 21:47 
Generaldrawing a form as the object Pin
The MadHatter4-Sep-03 8:01
memberThe MadHatter4-Sep-03 8:01 
GeneralRe: drawing a form as the object Pin
AK4-Oct-03 12:11
memberAK4-Oct-03 12:11 
GeneralComboBox Error Behavior Pin
Donny Lai1-Sep-03 19:25
memberDonny Lai1-Sep-03 19:25 
GeneralMaxDropDownItems Pin
Uriel3-Feb-03 20:47
memberUriel3-Feb-03 20:47 
GeneralThanks for the post Pin
Ian MacLean6-Mar-02 16:35
memberIan MacLean6-Mar-02 16:35 
GeneralRe: Thanks for the post Pin
omkamal30-Jul-03 7:30
memberomkamal30-Jul-03 7:30 
GeneralRe: Thanks for the post Pin
AniaD24-Oct-03 14:03
sussAniaD24-Oct-03 14:03 
GeneralPartially working Pin
tedda31-Jan-02 6:15
membertedda31-Jan-02 6:15 
GeneralRe: Partially working Pin
Neo Anderson31-Jan-02 7:44
memberNeo Anderson31-Jan-02 7:44 
GeneralRe: Partially working Pin
Senkwe Chanda31-Jan-02 20:42
memberSenkwe Chanda31-Jan-02 20:42 
GeneralRe: Partially working Pin
Senkwe Chanda1-Feb-02 3:32
memberSenkwe Chanda1-Feb-02 3:32 
GeneralRe: Partially working Pin
tedda1-Feb-02 3:58
membertedda1-Feb-02 3:58 
GeneralRe: Partially working Pin
Senkwe Chanda1-Feb-02 4:07
memberSenkwe Chanda1-Feb-02 4:07 
GeneralRe: Partially working Pin
mtg8281431-Jul-03 5:07
membermtg8281431-Jul-03 5:07 
GeneralRe: Partially working Pin
mtg8281431-Jul-03 5:27
membermtg8281431-Jul-03 5:27 
GeneralRe: Partially working Pin
AniaD27-Oct-03 9:52
sussAniaD27-Oct-03 9:52 
QuestionCode? Pin
Neo Anderson31-Jan-02 4:31
memberNeo Anderson31-Jan-02 4:31 
AnswerRe: Code? Pin
Senkwe Chanda31-Jan-02 4:36
memberSenkwe Chanda31-Jan-02 4:36 
GeneralRe: Code? Pin
Neo Anderson31-Jan-02 7:43
memberNeo Anderson31-Jan-02 7:43 
GeneralFull code please Pin
Neo Anderson30-Jan-02 9:01
memberNeo Anderson30-Jan-02 9:01 
GeneralRe: Full code please Pin
Senkwe Chanda30-Jan-02 20:12
memberSenkwe Chanda30-Jan-02 20:12 
GeneralRe: Full code please Pin
Robba13-Jan-04 5:04
memberRobba13-Jan-04 5:04 
GeneralMasks? + Nice but I'm not on .NET yet Pin
swinefeaster30-Jan-02 2:00
memberswinefeaster30-Jan-02 2:00 
GeneralRe: Masks? + Nice but I'm not on .NET yet Pin
Senkwe Chanda30-Jan-02 2:19
memberSenkwe Chanda30-Jan-02 2:19 
QuestionExtra Variables? Pin
James T. Johnson27-Jan-02 20:47
memberJames T. Johnson27-Jan-02 20:47 
AnswerRe: Extra Variables? Pin
Senkwe Chanda27-Jan-02 22:05
memberSenkwe Chanda27-Jan-02 22: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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 4 Feb 2002
Article Copyright 2002 by Senkwe Chanda
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid