应用场景:
由于我想在ComboBox的每一项上都绑定一小图标,但Visual Studio提供的ComboBox不能实现,所以自己找了方法重写了ComboBox控件,来实现和图标的绑定。代码如下:
public class ImageComboBoxItem:Object
{
#region 成员变量
private Color foreColor=Color.FromKnownColor(KnownColor.Transparent);
private bool bold = false;
private int imageIndex = -1;
private object tag = null;
private string text = null;
#endregion #region 构造函数
public ImageComboBoxItem()
{ } public ImageComboBoxItem(string Text)
{
text = Text;
} public ImageComboBoxItem(string Text, int ImageIndex):this(Text)
{
imageIndex = ImageIndex;
} public ImageComboBoxItem(string Text, int ImageIndex, bool Bold)
: this(Text, ImageIndex)
{
bold = Bold;
} public ImageComboBoxItem(string Text, int ImageIndex, bool Bold, Color ForeColor)
: this(Text, ImageIndex, Bold)
{
foreColor = ForeColor;
} public ImageComboBoxItem(string Text, int ImageIndex, bool Bold, Color ForeColor, Object Tag)
: this(Text, ImageIndex, Bold, ForeColor)
{
tag = Tag;
}
#endregion #region 属性
/// <summary>
/// 文本颜色
/// </summary>
public Color ForeColor
{
get { return foreColor; }
set { foreColor = value; }
} /// <summary>
/// 字体加粗
/// </summary>
public bool Bold
{
get { return bold; }
set { bold = value; }
} /// <summary>
/// 附加数据
/// </summary>
public Object Tag
{
get { return tag; }
set { tag = value; }
} /// <summary>
/// 图标索引
/// </summary>
public int ImageIndex
{
get { return imageIndex; }
set { imageIndex = value; }
} /// <summary>
/// 项目文本
/// </summary>
public string Text
{
get { return text; }
set { text = value; }
}
#endregion public override string ToString()
{
return text;
}
} /// <summary>
/// 带图标的ComoBox控件
/// </summary>
public class ImageComboBox : ComboBox
{
private ImageList imgs = new ImageList(); #region 构造函数
public ImageComboBox()
{
//设置绘制模式
this.DrawMode = DrawMode.OwnerDrawFixed;
}
#endregion #region 属性
public ImageList ImageList
{
get { return imgs; }
set { imgs = value; }
}
#endregion /// <summary>
/// 重写绘制Item的过程
/// </summary>
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
if (e.Index < 0)
{
e.Graphics.DrawString(this.Text, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top);
}
else
{
//是否ImageComboBoxItem
if (this.Items[e.Index].GetType() == typeof(ImageComboBoxItem))
{
ImageComboBoxItem item = (ImageComboBoxItem)this.Items[e.Index]; //获取颜色和字体
Color foreColor = (item.ForeColor != Color.FromKnownColor(KnownColor.Transparent)) ? item.ForeColor : e.ForeColor;
Font font = item.Bold ? (new Font(e.Font, FontStyle.Bold)) : e.Font; // -1:没有图标
if (item.ImageIndex != -1)
{
//画图标和文本
this.ImageList.Draw(e.Graphics, e.Bounds.Left, e.Bounds.Top, item.ImageIndex);
e.Graphics.DrawString(item.Text, font, new SolidBrush(foreColor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top);
}
else//画文本
e.Graphics.DrawString(item.Text, font, new SolidBrush(foreColor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top);
}
else
e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top);
base.OnDrawItem(e);
}
}
}遇到的问题:
在winform上添加该控件后,自己在ImageList属性里,加入图片时,总提示“参数必须是Image类型,参数名value”。所有图片类型都不行,各位大侠帮帮忙,谢啦
由于我想在ComboBox的每一项上都绑定一小图标,但Visual Studio提供的ComboBox不能实现,所以自己找了方法重写了ComboBox控件,来实现和图标的绑定。代码如下:
public class ImageComboBoxItem:Object
{
#region 成员变量
private Color foreColor=Color.FromKnownColor(KnownColor.Transparent);
private bool bold = false;
private int imageIndex = -1;
private object tag = null;
private string text = null;
#endregion #region 构造函数
public ImageComboBoxItem()
{ } public ImageComboBoxItem(string Text)
{
text = Text;
} public ImageComboBoxItem(string Text, int ImageIndex):this(Text)
{
imageIndex = ImageIndex;
} public ImageComboBoxItem(string Text, int ImageIndex, bool Bold)
: this(Text, ImageIndex)
{
bold = Bold;
} public ImageComboBoxItem(string Text, int ImageIndex, bool Bold, Color ForeColor)
: this(Text, ImageIndex, Bold)
{
foreColor = ForeColor;
} public ImageComboBoxItem(string Text, int ImageIndex, bool Bold, Color ForeColor, Object Tag)
: this(Text, ImageIndex, Bold, ForeColor)
{
tag = Tag;
}
#endregion #region 属性
/// <summary>
/// 文本颜色
/// </summary>
public Color ForeColor
{
get { return foreColor; }
set { foreColor = value; }
} /// <summary>
/// 字体加粗
/// </summary>
public bool Bold
{
get { return bold; }
set { bold = value; }
} /// <summary>
/// 附加数据
/// </summary>
public Object Tag
{
get { return tag; }
set { tag = value; }
} /// <summary>
/// 图标索引
/// </summary>
public int ImageIndex
{
get { return imageIndex; }
set { imageIndex = value; }
} /// <summary>
/// 项目文本
/// </summary>
public string Text
{
get { return text; }
set { text = value; }
}
#endregion public override string ToString()
{
return text;
}
} /// <summary>
/// 带图标的ComoBox控件
/// </summary>
public class ImageComboBox : ComboBox
{
private ImageList imgs = new ImageList(); #region 构造函数
public ImageComboBox()
{
//设置绘制模式
this.DrawMode = DrawMode.OwnerDrawFixed;
}
#endregion #region 属性
public ImageList ImageList
{
get { return imgs; }
set { imgs = value; }
}
#endregion /// <summary>
/// 重写绘制Item的过程
/// </summary>
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
if (e.Index < 0)
{
e.Graphics.DrawString(this.Text, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top);
}
else
{
//是否ImageComboBoxItem
if (this.Items[e.Index].GetType() == typeof(ImageComboBoxItem))
{
ImageComboBoxItem item = (ImageComboBoxItem)this.Items[e.Index]; //获取颜色和字体
Color foreColor = (item.ForeColor != Color.FromKnownColor(KnownColor.Transparent)) ? item.ForeColor : e.ForeColor;
Font font = item.Bold ? (new Font(e.Font, FontStyle.Bold)) : e.Font; // -1:没有图标
if (item.ImageIndex != -1)
{
//画图标和文本
this.ImageList.Draw(e.Graphics, e.Bounds.Left, e.Bounds.Top, item.ImageIndex);
e.Graphics.DrawString(item.Text, font, new SolidBrush(foreColor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top);
}
else//画文本
e.Graphics.DrawString(item.Text, font, new SolidBrush(foreColor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top);
}
else
e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top);
base.OnDrawItem(e);
}
}
}遇到的问题:
在winform上添加该控件后,自己在ImageList属性里,加入图片时,总提示“参数必须是Image类型,参数名value”。所有图片类型都不行,各位大侠帮帮忙,谢啦
解决方案 »
- String类中的format()函数的用法
- 如何把数组保存到配置文件里
- 帮看看这段C#翻译VB.net,转得正确吗?
- 自定义WEB服务器控件时,继承了某个WEB控件如CHECKBOX,怎么把某个属性在设计时变灰(就是不能设置其值)如Text..?
- 我做了一个简单的socket通讯的程序,但遇到了以下问题
- 如何判断一个Thread已经执行完毕。。。。。
- 视频开发问题?????????????????????????????????????
- 奇怪的属性问题
- 怎样读取、删除、更新SQLServer数据库的值
- 请教高手,sybase连接问题!
- C# web服务中如何获取TCP/IP属性栏里DNS
- 在c#想做个Treeview读出xml绑定数据
{
get { return imgs; }
set { imgs = value; }
}
imgUserNameSource.ImageList=ImageList1;
imgUserNameSource.Items.Add(new ImageComboBoxItem("pqmagic",0));
就可以了