为ComboBox的item设置图片
来源:互联网 发布:域名过户费用 编辑:程序博客网 时间:2024/04/28 15:48
class ComboBoxEx : ComboBox
{
private ImageList imageList;
public ImageList ImageList
{
get { return imageList; }
set { imageList = value; }
}
public ComboBoxEx()
{
DrawMode = DrawMode.OwnerDrawFixed;
}
protected override void OnDrawItem(DrawItemEventArgs ea)
{
ea.DrawBackground();
ea.DrawFocusRectangle();
ComboBoxExItem item;
Size imageSize = imageList.ImageSize;
Rectangle bounds = ea.Bounds;
try
{
item = (ComboBoxExItem)Items[ea.Index];
if (item.ImageIndex != -1)
{
imageList.Draw(ea.Graphics, bounds.Left+3, bounds.Top+3,item.ImageIndex);
ea.Graphics.DrawString(item.Text, ea.Font, new SolidBrush(ea.ForeColor), bounds.Left + imageSize.Width+3, bounds.Top+3);
}
else
{
ea.Graphics.DrawString(item.Text, ea.Font, new SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
}
catch
{
if (ea.Index != -1)
{
ea.Graphics.DrawString(Items[ea.Index].ToString(), ea.Font, new SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
else
{
ea.Graphics.DrawString(Text, ea.Font, new SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
}
base.OnDrawItem(ea);
}
}
class ComboBoxExItem
{
private string _text;
public string Text
{
get { return _text; }
set { _text = value; }
}
private int _imageIndex;
public int ImageIndex
{
get { return _imageIndex; }
set { _imageIndex = value; }
}
public ComboBoxExItem()
: this(" ")
{
}
public ComboBoxExItem(string text)
: this(text, -1)
{
}
public ComboBoxExItem(string text, int imageIndex)
{
_text = text;
_imageIndex = imageIndex;
}
public override string ToString()
{
return _text;
}
}
private void Form1_Load(object sender, EventArgs e)
{
ComboBoxEx cbo = new ComboBoxEx();
cbo.ImageList = imageList1; //imageList1 设置图片12*12
cbo.Items.Add(new ComboBoxExItem("A1",0));
cbo.Items.Add(new ComboBoxExItem("A2",1));
this.Controls.Add(cbo); //放入窗体
}
- 为ComboBox的item设置图片
- ComboBox的Item
- comboBox 的DropDownStyle 要设置为“dropdownList”
- 为ComboBox加图片
- 设置GridView的item为正方形
- 为ComboBox控件添加图片
- 控件显示内容的绑定类 combobox 为每一item 加tag
- 如何设置已经创建的ComBoBox为CBS_DROPDOWNLIST样式
- jqery 用 readonly 设置easyui-combobox为只读的方法
- listview为不同的item设置不同的点击事件
- Android为ListView的Item设置不同的布局
- Android为ListView的Item设置不同的布局
- Android为ListView的Item设置不同的布局
- Android为ListView的Item设置不同的布局
- getItemViewType为Listview的item设置不同的布局
- UITabBarController知识设置item图片
- Android为ListView的Item设置不同颜色背景
- 用 Case方法为每个 ListView 的Item设置监听器
- 网购技巧
- Windows 7 正版驗證機制加油,又有 Hacker 以直接了當的方式破解了。
- 如何派生在类向导中不存在的类
- 电源接口脱落:只需120,为何戴尔报价1500?
- PL SQL problem Set
- 为ComboBox的item设置图片
- hdoj1071 The area
- MyEclipse j2me开发环境配置
- HOOK钩子机制学习笔记(1)
- 动态地往datagirdview中添加数据,当所添加的数据超过datagridview的显示范围后,滚动条就无法显示,程序出现假死状态,(scrollbars=both)
- Paradox of our times
- Windows 2000/XP/2003修改输入法列表排列顺序的办法
- DataGridView为行动态添加背景色
- 快速完成和读懂测试计划