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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With