实现此功能主要通过ComboBox控件的DrawMode属性、DropDownStyle属性以及DrawItem事件和Graphic类的公共属性和方法完成。首先把DrawMode属性设置为OwnerDrawFixed,把DropDownStyle属性设置为DropDownList,然后在控件DrawItem事件下添加图片。主要代码如下:
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.DrawItem+=new DrawItemEventHandler(comboBox1_DrawItem);
comboBox1.Items.Add("123");
comboBox1.Items.Add("456");
comboBox1.Items.Add("789");
for (int i = 0; i < comboBox1.Items.Count+1; i++)
{
imageList1.Images.Add(imageList1.Images[0]);
//必须保证图片数大于等于选项数目
}
}
private void comboBox1_DrawItem(object
如何实现ComboBox下拉列表显示图片
最新推荐文章于 2020-07-24 11:44:37 发布
本文介绍了如何在C#的ComboBox控件中实现下拉列表显示图片的功能。通过设置ComboBox的DrawMode为OwnerDrawFixed,DropDownStyle为DropDownList,并利用DrawItem事件及Graphics类的方法,可以在每个选项前添加图片。代码示例展示了添加图片、绘制背景及文字等步骤。
摘要由CSDN通过智能技术生成