Search Forum
(57415 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


              
Printable Version

ComboBox With Images
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;
    }
  }
}