分为下面几步:
1.添加一个类;
2.给类添加一个继承ComboBox;
3.从工具箱中拖出一个ComboBox添加的面板,如附件中的图片所示;
3.添加需要的代码。
sing System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace CreateGDB.ThemeMap
{
// <summary>
/// 重写了Combobox类,使其在下拉框中显示Symbol颜色带的图片
/// </summary>
/// <remarks>
/// </remarks>
public partial class PictureComboBox : ComboBox
{
private ComboBox comboBox1;
public PictureComboBox()
{
//以下两句是关键的;
DrawMode = DrawMode.OwnerDrawFixed;
DropDownStyle. = ComboBoxStyle.DropDownList;
}
//重写函数
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
try
{
//显示图片
Image image = (Image)Items[e.Index];
System.Drawing.Rectangle rect = e.Bounds;
e.Graphics.DrawImage(image, rect);
}
catch (Exception ex)
{
throw ex;
}
finally
{
base.OnDrawItem(e);
}
}
private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(0, 0);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 0;
this.ResumeLayout(false);
}
}
}