Reputation: 3710
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:
After item is chosen:
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!
Upvotes: 8
Views: 4115
Reputation: 11238
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.
Upvotes: 7
Reputation: 8172
You also have to override OnPaint in a similar fashion to your OnDrawItem method for this to work.
Upvotes: 0