Hi guys,

I am trying to change color of the existing items in my listbox. Can somebody please help?

Dani AI

Generated

— quick diagnosis and a simple fix. When you switch a WinForms ListBox to owner-draw, the control stops painting item text for you. That means changing an item's color later only works if your DrawItem handler chooses the color at draw time (it won't pick up any implicit per-item ForeColor unless you implement one). As requested, showing how to store a color per item and force a redraw makes this reliable.

A compact pattern: keep a small item class with text + color, let DrawItem pick the color (and respect selection), and call Invalidate/GetItemRectangle after you change a color. Example:

class ColoredItem
{
    public string Text { get; set; }
    public Color Color { get; set; }
    public ColoredItem(string text, Color color) { Text = text; Color = color; }
    public override string ToString() { return Text; }
}

/* in setup: set DrawMode to OwnerDrawFixed, add ColoredItem instances, wire DrawItem */

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index < 0) return;
    e.DrawBackground();

    var item = listBox1.Items[e.Index] as ColoredItem;
    Color fore = ((e.State & DrawItemState.Selected) != 0)
        ? SystemColors.HighlightText
        : (item?.Color ?? listBox1.ForeColor);

    using (var brush = new SolidBrush(fore))
    {
        e.Graphics.DrawString(item?.Text ?? listBox1.Items[e.Index].ToString(),
                              e.Font, brush, e.Bounds.Location);
    }
    e.DrawFocusRectangle();
}

/* change color and refresh one item */
((ColoredItem)listBox1.Items[0]).Color = Color.Red;
listBox1.Invalidate(listBox1.GetItemRectangle(0));

Troubleshooting tips: always check e.Index >= 0; call e.DrawBackground and e.DrawFocusRectangle so selection and focus render correctly; use using for brushes to avoid leaks; if the list is data-bound, call the binding source's ResetBindings(false) or Refresh after updates. If you want a simpler control with built-in per-item ForeColor, consider ListView/ListViewItem which exposes ForeColor directly.

Recommended Answers

All 2 Replies

You have to put some of your code here, you have to show what you ve done so far, after that I am pretty sure that someone will help you out with your problem.

Below method works fine for adding new items to the listbox but not in case when I am trying to change the forecolor of an existing item in a listbox.

    private void Form1_Load(object sender, EventArgs e)
    {
        listBox1.Items.Add("First");
        listBox1.Items.Add("Second");
        listBox1.DrawMode = DrawMode.OwnerDrawFixed;
        listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);
    }

    void listBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, Brushes.Red, e.Bounds);

    }

Hope this clarifies my approach?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.