Spread 控件提供一系列的图形单元格类型,通过这些单元格类型我们可以在单元格中嵌入控件或窗体和单元格交互。例如,你可以使用 ButtonCellType 显示按钮在单元格中,使用 ColorPickerCellType 在单元格中显示颜色选择器。
这些单元格其中之一为 ComboBoxCellType。ComboBox 提供了一个可编辑的下拉列表。用户可以直接填写文本内容或者从下拉列表中选择文本。
我们可以通过以下属性设置 ComboBoxCellType 下拉列表数据:
- Items: 下拉列表中显示的文本
- ItemData: 下拉列表中显示文本的实际值。
- ImageList: 在显示文本旁显示图片。
使用以上属性,你可以展示带有图片的文字选项。实现该功能,我们可以通过继承 ComboBoxCellType 实现。核心代码如下:
public class ImageCombo : FarPoint.Win.Spread.CellType.ComboBoxCellType
{
public override void PaintCell(Graphics g, Rectangle r, FarPoint.Win.Spread.Appearance appearance, object value, bool isSelected, bool isLocked, float zoomFactor)
{
if (value != null)
{
int ind =0;
for (int i = 0; i < base.Items.Length; i++)
{
if (base.Items<em></em> == value.ToString()
{
ind = i;
break;
}
}
Image img = base.ImageList.Images[ind];
g.DrawImage(img, new Rectangle(new Point(r.X, r.Y), new Size(20, 20)));
g.DrawString(value.ToString(), appearance.Font, new SolidBrush(Color.Black), new PointF(r.X + 20, r.Y-10 + 10));
ControlPaint.DrawComboButton(g, new Rectangle(r.Right - 17, r.Y, 17, r.Height), ButtonState.Normal);
}
else
{
base.PaintCell(g, r, appearance, value, isSelected, isLocked, zoomFactor);
}
}
}
效果如图:
下载实例:
VS2010 && Framework 4.0 && Spread for .NET 6