Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing Images within a Custom ComboBox

For a current project, I needed a dropdown menu with color names (strings) with a small example square of the color next to it (image). So, I was able to design a custom ComboBox to achieve this. However, I'm having one problem....when I select an item from the list, the color example doesn't show up, only the name of the color does. (See examples below)

Expanded Menu:

alt text

After item is chosen:

alt text

In order to draw the colors next to the strings to begin with, I used:

    // Draws the items into the ColorSelector object
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        e.DrawBackground();
        e.DrawFocusRectangle();

        DropDownItem item = (DropDownItem)Items[e.Index];
        // Draw the colored 16 x 16 square
        e.Graphics.DrawImage(item.Image, e.Bounds.Left, e.Bounds.Top);
        // Draw the value (in this case, the color name)
        e.Graphics.DrawString(item.Value, e.Font, new
                SolidBrush(e.ForeColor), e.Bounds.Left + item.Image.Width, e.Bounds.Top + 2);

        base.OnDrawItem(e);
    }

Where a DropDownItem contained the image and the string to be drawn. So...does anyone know what I need to override or what I need to do in order to get the ComboBox to draw the image and the string both, just like it already does when the list is expanded, when an item is selected?

Thanks a lot; Cheers!

like image 695
JToland Avatar asked Nov 17 '10 18:11

JToland


1 Answers

Set DropDownStyle to DropDownList; by default ComboBox uses a TextBox to display the selected item. This is why the selected item displays differently from the drop down items.

like image 167
lincolnk Avatar answered Oct 17 '22 20:10

lincolnk