Placing Images and Strings with a C# Combobox

前端 未结 5 1585
傲寒
傲寒 2020-12-01 14:30

I need to create a dropdown menu, or combobox, for a Windows Forms application which contains a small image and then a string of text next to it. Basically, you can think o

5条回答
  •  [愿得一人]
    2020-12-01 15:27

    NOTE: This code is from user que dal's optimization If you wish to have a string that isn't just the name of the color, change DropDownItem to have 2 arguments, the string and the color, then just change how the brush sets the color, as such:

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

    You must then change the dropdown item as such:

        public DropDownItem()
            : this("", Color.Empty)
        {}
    

    Hope this was helpful :)

提交回复
热议问题