Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background color of a ListBox item (Windows Forms)

How can I set the background color of a specific item in a System.Windows.Forms.ListBox?

I would like to be able to set multiple ones if possible.

like image 939
Dested Avatar asked Sep 18 '08 11:09

Dested


People also ask

How do I change the background color in Windows form?

Set the background in the Windows Forms DesignerOpen the project in Visual Studio and select the Panel control. In the Properties window, click the arrow button next to the BackColor property to display a window with three tabs. Select the Custom tab to display a palette of colors.

How do I change the color of a button in Windows Forms?

Step 2: Drag the Button control from the ToolBox and drop it on the windows form. You are allowed to place a Button control anywhere on the windows form according to your need. Step 3: After drag and drop you will go to the properties of the Button control to set the BackColor property of the Button.


2 Answers

Thanks for the answer by Grad van Horck. It guided me in the correct direction.

To support text (not just 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 the given code and will show the proper text plus highlight the selected item.

like image 130
Shadow Wizard Hates Omicron Avatar answered Sep 22 '22 18:09

Shadow Wizard Hates Omicron


Probably the only way to accomplish that is to draw the items yourself.

Set the DrawMode to OwnerDrawFixed and code something like this on 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 would be using a ListView, although they have an other way of implementations (not really data bound, but more flexible in way of columns).

like image 39
Grad van Horck Avatar answered Sep 23 '22 18:09

Grad van Horck