Assuming this is
the Windows Forms ListBox
control[
^], you need to set
the DrawMode
[
^] to
OwnerDrawFixed
and handle
the DrawItem
event[
^]. MSDN has an example:
private ListBox ListBox1 = new ListBox();
private void InitializeListBox()
{
ListBox1.Items.AddRange(new Object[]
{ "Red Item", "Orange Item", "Purple Item" });
ListBox1.Location = new System.Drawing.Point(81, 69);
ListBox1.Size = new System.Drawing.Size(120, 95);
ListBox1.DrawMode = DrawMode.OwnerDrawFixed;
ListBox1.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem);
Controls.Add(ListBox1);
}
private void ListBox1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
e.DrawBackground();
Brush myBrush = Brushes.Black;
switch (e.Index)
{
case 0:
myBrush = Brushes.Red;
break;
case 1:
myBrush = Brushes.Orange;
break;
case 2:
myBrush = Brushes.Purple;
break;
}
e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(),
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
e.DrawFocusRectangle();
}