I am using code from msdn so that entries in my list box change color based on their index position. The problem I am having is that the entries do not take on new colors, and it seems that the lbAppointmentDates_DrawItem function never gets called (I put a message box in the code to test this).
Here is my code
this.lbAppointmentDates.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.lbAppointmentDates_DrawItem);
private void lbAppointmentDates_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
// Set the DrawMode property to draw fixed sized items.
lbAppointmentDates.DrawMode = DrawMode.OwnerDrawFixed;
// Draw the background of the ListBox control for each item.
e.DrawBackground();
// Create a new Brush and initialize to a Black colored brush by default.
Brush myBrush = Brushes.Black;
MessageBox.Show(e.Index.ToString());
// Determine the color of the brush to draw each item based on the index of the item to draw.
switch (e.Index)
{
case 0:
myBrush = Brushes.Red;
break;
case 1:
myBrush = Brushes.Orange;
break;
case 2:
myBrush = Brushes.Purple;
break;
}
// Draw the current item text based on the current Font and the custom brush settings.
e.Graphics.DrawString(lbAppointmentDates.Items[e.Index].ToString(), e.Font, myBrush,e.Bounds,StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
Can anyone tell me why this might not be working?
Thanks,
Paul