Spread : 在 ComboBoxCellType 中显示图片

发布时间:2013/03/14 00:03 发布者:iceman

返回博客中心

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); 
       } 
  } 
}

效果如图:

4

下载实例:

VS2010 && Framework 4.0 && Spread for .NET 6

Images In Combo C#
Images in Combo Vb


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网