Click here to Skip to main content
Licence CPOL
First Posted 1 Feb 2010
Views 10,414
Bookmarked 20 times

Show Image in Combo Box

By | 3 Feb 2010 | Technical Blog
Add images in combo box
A Technical Blog article. View original blog here.[^]

It is very easy to add images in combo box. .NET provides an easy way to add an image. Here is an example code.

 public static void BindTaskPriorityCombo(ComboBox priorityCombo, bool addEmpty)
 {
    priorityCombo.DrawMode = DrawMode.OwnerDrawVariable;
    priorityCombo.DrawItem += new DrawItemEventHandler(priorityCombo_DrawItem);
    priorityCombo.Items.Clear();

    if (addEmpty)
    {
        priorityCombo.Items.Add("");
    }

    string[] priorities = {"High", "Medium", "Low"};
    foreach (string priority in priorities )
    {
        priorityCombo.Items.Add(priority);
    }
 }
 static void priorityCombo_DrawItem(object sender, DrawItemEventArgs e)
 {
    if (e.Index >= 0)
    {
        ComboBox cmbPriority = sender as ComboBox;
        string text = cmbPriority.Items[e.Index].ToString();
        e.DrawBackground();

        if (text.Length > 0)
        {
            string priority = text;
            Image img = GetTaskPriorityImage(priority);

            if (img != null)
            {
                e.Graphics.DrawImage(img, e.Bounds.X, e.Bounds.Y, 15, 15);
            }
        }

        e.Graphics.DrawString(text, cmbPriority.Font, System.Drawing.Brushes.Black,
                                    new RectangleF(e.Bounds.X + 15, e.Bounds.Y,
                                    e.Bounds.Width, e.Bounds.Height));

        e.DrawFocusRectangle();
    }
 }
 public static Image GetTaskPriorityImage(string priority)
 {
    switch (priority)
    {
        case "High":
        {
            return Properties.Resources.High_Priority;
        }
        case "Low":
        {
            return Properties.Resources.Low_Priority;
        }
        case "Medium":
        {
            return Properties.Resources.Medium_Priority;
        }
    }

    return null;

}

Call BindTaskPriorityCombo() method to bind priority combo box. Set priorityCombo.DrawMode = DrawMode.OwnerDrawVariable and register Draw item event of combo box using priorityCombo.DrawItem += new DrawItemEventHandler(priorityCombo_DrawItem) from drawing image in combo box item.

For more, read my article: Task Manager.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Shakeel Iqbal

Software Developer (Senior)
TEO
Pakistan Pakistan

Member

i have been working in software house as senior software engineer. i have worked on many web and windows projects. My hobbies are to read books and develop my own small utilities.
 

My Blogs
Follow me on Twitter

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 PinmemberMark Guo12:35 1 Feb '11  
GeneralMy vote of 5 PinmemberMember 62925520:26 19 Jul '10  

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
Web02 | 2.5.120517.1 | Last Updated 3 Feb 2010
Article Copyright 2010 by Shakeel Iqbal
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid