ListBox element background color (winforms)

How to set the background color for a specific item in System.Windows.Forms.ListBox? I would like to be able to install several if possible.

+46
c # colors winforms listbox
Sep 18 '08 at 11:24
source share
5 answers

Probably the only way to achieve this is to draw the elements yourself.

Set DrawMode to OwnerDrawFixed

and enter code of this type in the DrawItem event:

 private void listBox_DrawItem(object sender, DrawItemEventArgs e) { e.DrawBackground(); Graphics g = e.Graphics; g.FillRectangle(new SolidBrush(Color.Silver), e.Bounds); // Print text e.DrawFocusRectangle(); } 

The second option will use a ListView, although they have a different implementation method (not actually data-related, but more flexible as columns)

+51
Sep 18 '08 at 11:28
source share

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.

+54
Sep 14 '10 at 13:46
source share
 // Set the background to a predefined colour MyListBox.BackColor = Color.Red; // OR: Set parts of a color. MyListBox.BackColor.R = 255; MyListBox.BackColor.G = 0; MyListBox.BackColor.B = 0; 

If you want to set several background colors, set a different background color for each item, this is not possible in the ListBox, but IS with a ListView with something like:

 // Set the background of the first item in the list MyListView.Items[0].BackColor = Color.Red; 
+2
Sep 18 '08 at 11:31
source share
 public Picker() { InitializeComponent(); this.listBox.DrawMode = DrawMode.OwnerDrawVariable; this.listBox.MeasureItem += listBoxMetals_MeasureItem; this.listBox.DrawItem += listBoxMetals_DrawItem; } void listBoxMetals_DrawItem(object sender, DrawItemEventArgs e) { e.DrawBackground(); Brush myBrush = Brushes.Black; var item = listBox.Items[e.Index] as Mapping; if (e.Index % 2 == 0) { e.Graphics.FillRectangle(new SolidBrush(Color.GhostWhite), e.Bounds); } e.Graphics.DrawString(item.Name, e.Font, myBrush, e.Bounds, StringFormat.GenericDefault); e.DrawFocusRectangle(); } 

Full sample

0
Jul 09 '14 at 6:55
source share
 public MainForm() { InitializeComponent(); this.listbox1.DrawItem += new DrawItemEventHandler(this.listbox1_DrawItem); } private void listbox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { e.DrawBackground(); Brush myBrush = Brushes.Black; var item = listbox1.Items[e.Index]; if(e.Index % 2 == 0) { e.Graphics.FillRectangle(new SolidBrush(Color.Gold), e.Bounds); } e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), e.Font, myBrush,e.Bounds, StringFormat.GenericDefault); e.DrawFocusRectangle(); } } 
0
Jan 13 '17 at 9:44 on
source share



All Articles