Thanks for the reply from Grad van Horck , he directed me in the right direction.
To support the text (and not just the background color), here is my fully working code:
//global brushes with ordinary/selected colors private SolidBrush reportsForegroundBrushSelected = new SolidBrush(Color.White); private SolidBrush reportsForegroundBrush = new SolidBrush(Color.Black); private SolidBrush reportsBackgroundBrushSelected = new SolidBrush(Color.FromKnownColor(KnownColor.Highlight)); private SolidBrush reportsBackgroundBrush1 = new SolidBrush(Color.White); private SolidBrush reportsBackgroundBrush2 = new SolidBrush(Color.Gray); //custom method to draw the items, don't forget to set DrawMode of the ListBox to OwnerDrawFixed private void lbReports_DrawItem(object sender, DrawItemEventArgs e) { e.DrawBackground(); bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected); int index = e.Index; if (index >= 0 && index < lbReports.Items.Count) { string text = lbReports.Items[index].ToString(); Graphics g = e.Graphics; //background: SolidBrush backgroundBrush; if (selected) backgroundBrush = reportsBackgroundBrushSelected; else if ((index % 2) == 0) backgroundBrush = reportsBackgroundBrush1; else backgroundBrush = reportsBackgroundBrush2; g.FillRectangle(backgroundBrush, e.Bounds); //text: SolidBrush foregroundBrush = (selected) ? reportsForegroundBrushSelected : reportsForegroundBrush; g.DrawString(text, e.Font, foregroundBrush, lbReports.GetItemRectangle(index).Location); } e.DrawFocusRectangle(); }
The above adds to this code and displays the correct text plus the selection of the selected item.
Shadow Wizard Sep 14 '10 at 13:46 2010-09-14 13:46
source share