-1

I'm using the following code to display image representing a color in a combobox as per this SO answer https://stackoverflow.com/a/13385209/848968 I have added the custom control to the form,but i cannot figure out how to add items with images to it.Kindly advice.

  public sealed class ColorSelector : ComboBox
    {
        public ColorSelector()
        {
            DrawMode = DrawMode.OwnerDrawFixed;
            DropDownStyle = ComboBoxStyle.DropDownList;
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            e.DrawBackground();

            e.DrawFocusRectangle();

            if (e.Index >= 0 && e.Index < Items.Count)
            {
                DropDownItem item = (DropDownItem)Items[e.Index];

                e.Graphics.DrawImage(item.Image, e.Bounds.Left, e.Bounds.Top);

                e.Graphics.DrawString(item.Value, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + item.Image.Width, e.Bounds.Top + 2);
            }

            base.OnDrawItem(e);
        }
    }

public sealed class DropDownItem
{
    public string Value { get; set; }

    public Image Image { get; set; }

    public DropDownItem()
        : this("")
    { }



 public DropDownItem(string val)
    {
        Value = val;
        Image = new Bitmap(16, 16);
        using (Graphics g = Graphics.FromImage(Image))
        {
            using (Brush b = new SolidBrush(Color.FromName(val)))
            {
                g.DrawRectangle(Pens.White, 0, 0, Image.Width, Image.Height);
                g.FillRectangle(b, 1, 1, Image.Width - 1, Image.Height - 1);
            }
        }
    }

    public override string ToString()
    {
        return Value;
    }
}
techno
  • 6,100
  • 16
  • 86
  • 192
  • Try something like this one: [Combobox draw image on selected](https://stackoverflow.com/a/50103139/7444103). It's a standard (flat) ComboBox that uses an ImageList as source for the images . Pretty simple to implement. But, if you need to just represent a Color, you could just fiil a rectangle with a Color of choice. See [this one](https://stackoverflow.com/a/53074638/7444103), just the animation, where you can see the ComboBoxes used as Color selector. Is it this kind of result you're looking for? – Jimi Jan 21 '19 at 10:43
  • please check my answer – Abinash Jan 23 '19 at 03:13

1 Answers1

0

Based on your requirement, I have modified OnDrawItem method and changed the comboBox width and height to clear image visible

Please find the code below

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        if (e.Index >= 0  )
        {
            e.DrawBackground();
            e.DrawFocusRectangle();

            string imageFilePath = @Items[e.Index].ToString();;
            int width = 40;
            int height = 20;

            Image img = Image.FromFile(imageFilePath);
            e.Graphics.DrawImage(img, 0, e.Bounds.Top + 2, width, height);
            e.Graphics.DrawString(imageFilePath, e.Font, new
                    SolidBrush(e.ForeColor), e.Bounds.Left + width, e.Bounds.Top + 2);
            base.OnDrawItem(e);
        }

    }

}

output :

See the image attached

Abinash
  • 471
  • 3
  • 13