Introduction
When I started programming in .NET, I wondered where the ImageCombo
control had gone (I used to program in VB6). The .NET Framework didn't seem to have this functionality built-in, but it's very easy to add it.
Using the code
The code consists of two classes: ImageCombo
and ImageComboItem
. If you want to use them in C#, just copy them into your project; for other .NET languages, you can add a reference to the library.
The control inherits from ComboBox
and introduces one new member: the ImageList
property, which doesn't require any further explanation. The control is owner drawn and there is a custom drawing method defined, so don't change its DrawMode
property if you want to see the images.
The ImageComboItem
class inherits from Object
. You can set a customForeColor
and it has a Mark
property which determines if the item is shown in bold font style (does not work if owner font is already bold).
To add an item to an ImageCombo
with text "Icon 0" and image index 0, use the following code:
imageCombo.Items.Add(new ImageComboItem("Icon 0", 0));
Code listing: ImageCombo
class
using System;
using System.Drawing;
namespace System.Windows.Forms
{
public class ImageCombo : ComboBox
{
private ImageList imgs = new ImageList();
public ImageCombo()
{
this.DrawMode = DrawMode.OwnerDrawFixed;
}
public ImageList ImageList
{
get
{
return imgs;
}
set
{
imgs = value;
}
}
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
{
if (this.Items[e.Index].GetType() == typeof(ImageComboItem))
{
ImageComboItem item = (ImageComboItem)
this.Items[e.Index];
Color forecolor = (item.ForeColor !=
Color.FromKnownColor(KnownColor.Transparent)) ?
item.ForeColor : e.ForeColor;
Font font = item.Mark ? new Font(e.Font,
FontStyle.Bold) : e.Font;
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);
}
}
}
Code listing: ImageComboItem
class
using System;
using System.Drawing;
namespace System.Windows.Forms
{
public class ImageComboItem : object
{
private Color forecolor = Color.FromKnownColor(
KnownColor.Transparent);
private bool mark = false;
private int imageindex = -1;
private object tag = null;
private string text = null;
public ImageComboItem()
{
}
public ImageComboItem(string Text)
{
text = Text;
}
public ImageComboItem(string Text, int ImageIndex)
{
text = Text;
imageindex = ImageIndex;
}
public ImageComboItem(string Text, int ImageIndex, bool Mark)
{
text = Text;
imageindex = ImageIndex;
mark = Mark;
}
public ImageComboItem(string Text, int ImageIndex,
bool Mark, Color ForeColor)
{
text = Text;
imageindex = ImageIndex;
mark = Mark;
forecolor = ForeColor;
}
public ImageComboItem(string Text, int ImageIndex,
bool Mark, Color ForeColor, object Tag)
{
text = Text;
imageindex = ImageIndex;
mark = Mark;
forecolor = ForeColor;
tag = Tag;
}
public Color ForeColor
{
get
{
return forecolor;
}
set
{
forecolor = value;
}
}
public int ImageIndex
{
get
{
return imageindex;
}
set
{
imageindex = value;
}
}
public bool Mark
{
get
{
return mark;
}
set
{
mark = value;
}
}
public object Tag
{
get
{
return tag;
}
set
{
tag = value;
}
}
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
public override string ToString()
{
return text;
}
}
}
History
- Original code (01/10/2004)
- Optimized code for speed & added some functionality (18/10/2004)