问题:
(1)如何往WinForm中的下拉列表框中添加图片:既前面为图片后面为文字,
而且图片可以通过编程方法修改。
(2):有点同上:不过问题是。在下拉列表框中实现类似于TreeView中的效果.
分级显示.
知道的朋友说说。
最好能有源码。
光说也不明白。
(1)如何往WinForm中的下拉列表框中添加图片:既前面为图片后面为文字,
而且图片可以通过编程方法修改。
(2):有点同上:不过问题是。在下拉列表框中实现类似于TreeView中的效果.
分级显示.
知道的朋友说说。
最好能有源码。
光说也不明白。
By Rakka Rage There is no built in support for a ComboBox with images. So i created a simple ComboBoxEx class that derives from ComboBox and allows you to set the ComboBoxEx.ImageList property and ComboBoxExItem.ImageIndex property so that the ComboBox displays an image. ...
ComboBoxEx comboBox = new ComboBoxEx();
comboBox.ImageList = imageList;
// not needed but... no icon for index -1 else
comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
// just pass these in instead of strings, class included below
// specify a valid imageIndex
comboBox.Items.Add(new ComboBoxExItem("Text0", 0));
comboBox.Items.Add(new ComboBoxExItem("Text1", 1));Code:
namespace Rage.Library
{
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, bounds.Top,
item.ImageIndex);
ea.Graphics.DrawString(item.Text, ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left+imageSize.Width, bounds.Top);
}
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;
}
}
}
而,我希望图片能够在三种模式下都显示在Textbox中。本文章将给出的ImageComboBox,可以在三种模式下,把你在列表框中选中的图片和文字显示到Combobox的文本框中。另外,此ImageComboBox还支持为每个item划分多层次,它有助于对item进行分组,显示相互的层次关系。当DrawMode被设置成OwnerDrawVariable时,每个item还可以有各自的字体,各自的文字大小。因此,每个在Combobox中的item有几个属性,这些属性都可以静态或动态地指定。然后,这些item就不再只是简单的字符串,而是特殊的ImageComboBoxItem对象。因此,这些item是一个ImageComboBoxItem对象的集合。