c# listboxitem - Background color of a ListBox item(winforms)





the programmatically (5)


// 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 what you mean by setting multiple background colors is setting a different background color for each item, this isn't possible with a 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;

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.




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();
        }


public MainForm()
        {
            InitializeComponent();
            this.listbox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listbox1_DrawItem);
        }



     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();
    }

Complete sample




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 selected item.




Assuming you mean the HTML type RGB codes (called Hex codes, such as #FFCC66), use the ColorTranslator class:

System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#FFCC66");

If, however you are using an ARGB hex code, you can use the ColorConverter class from the System.Windows.Media namespace:

Color col = ColorConverter.ConvertFromString("#FFDFD991") as Color;
//or      = (Color) ColorConverter.ConvertFromString("#FFCC66") ;




c# winforms listbox colors