在自定义组合框中绘制图像

JTo*_*and 8 c# user-controls drawing combobox image

对于当前项目,我需要一个带有颜色名称(字符串)的下拉菜单,其旁边有一个小的示例方块(图像).所以,我能够设计一个自定义的ComboBox来实现这一目标.但是,我遇到了一个问题....当我从列表中选择一个项目时,颜色示例没有显示,只有颜色的名称.(见下面的例子)

扩展菜单:

替代文字

选择项目后:

替代文字

为了首先绘制字符串旁边的颜色,我使用了:

    // 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);
    }
Run Code Online (Sandbox Code Playgroud)

DropDownItem包含图像和要绘制的字符串.那么......有没有人知道我需要覆盖什么或者我需要做什么才能让ComboBox绘制图像和字符串两者,就像扩展列表时选择项目时一样?

非常感谢; 干杯!

lin*_*lnk 7

设置DropDownStyleDropDownList; 默认情况下ComboBox使用a TextBox来显示所选项目.这就是所选项目与下拉项目显示不同的原因.