欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

comboBox控件下拉选项中添加图片

程序员文章站 2022-04-12 11:49:47
...

comboBox下拉选项中添加图片

1、将需要添加的图片加入到imageList中
2、循环图片清单,将图片绘入comboBox中,代码如下:

		private void FormMain_Load(object sender, EventArgs e)
            foreach (string key in imageList1.Images.Keys)
            {
                comboBox1.Items.Add(key);
            }
            comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox1.DrawItem += CbbImages_DrawItem;
            comboBox1.SelectedIndex = 0;
		}
		
        private void CbbImages_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.Graphics.DrawImage(imageList1.Images[e.Index], 5, e.Bounds.Y, 40, 40);
            e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), new Font("宋体", 16), new SolidBrush(Color.Red), 45, e.Bounds.Y + 1);
        }

相关标签: combobox