The video below gives an overview of creating a ComboBox that does conditional formatting. The code uses regular expressions to determine if an item in a combobox should have special formatting. The code is nowhere near production ready, but it should give you an idea of how to do formatting of items in a combobox drop down list. See the full post if you want to copy the code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.ComponentModel;
namespace CBContainer
{
class MyComboBox : ComboBox
{
public FormattingOptions[] FormatList { get; set; }
public MyComboBox()
{
this.DrawMode = DrawMode.OwnerDrawFixed;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
// Have the base do all of its work
base.OnDrawItem(e);
FormattingOptions oFormat = null;
for (int i = 0; i < FormatList.Length; i++)
{
// Make sure we have a regex string to try.
if (FormatList[i].RegularExpression != "")
{
// Create the regular expression
Regex oReg = new Regex(FormatList[i].RegularExpression);
// Check if it is a match
if (oReg.IsMatch(this.Items[e.Index].ToString()))
{
// Set the format from the list
oFormat = FormatList[i];
// Break out of the loop
break;
}
}
}
// Check if we found a format to use
if (oFormat != null)
{
// Make sure we have a valid font
if (oFormat.ItemFont == null)
oFormat.ItemFont = this.Font;
// Draw the item
e.Graphics.DrawString(this.Items[e.Index].ToString(), oFormat.ItemFont, new SolidBrush(oFormat.ForeColor), e.Bounds);
}
else
{
// Draw the item with the default settings
e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, new SolidBrush(this.ForeColor), e.Bounds);
}
}
}
class FormattingOptions
{
[Description("This is the font of the item.")]
public Font ItemFont { get; set; }
public Color ForeColor { get; set; }
public string RegularExpression { get; set; }
public override string ToString()
{
return RegularExpression;
}
}
}