What the...?

Something bad happened

We're not sure what, but we have a few guesses.

Problem: Object reference not set to an instance of an object.
Ticket: 7820484
Server: Web04
Show Image in Combo Box - CodeProject Click here to Skip to main content
11,576,354 members (54,520 online)
Click here to Skip to main content

Tagged as

Show Image in Combo Box

, 3 Feb 2010 CPOL 16.1K 21
Rate this:
Please Sign up or sign in to vote.
Add images in combo box

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)

Share

About the Author

Shakeel Iqbal
Software Developer (Senior) TEO
Pakistan Pakistan
Passion and positive dedication is essential part of success. I believe on hardworking and sharing knowledge with others. I always try to be a better than I am and think positive for positive result.

My Blogs

Comments and Discussions

 
GeneralMy vote of 5 Pin
Mark Guo1-Feb-11 12:35
memberMark Guo1-Feb-11 12:35 
GeneralMy vote of 5 Pin
Member 62925519-Jul-10 20:26
memberMember 62925519-Jul-10 20:26 

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.150603.1 | Last Updated 3 Feb 2010
Article Copyright 2010 by Shakeel Iqbal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid