Reputation: 101
I want to change the color of a ListBox
items. The code I have doesn't seem to work.
It just adds the namespace of the class to the ListBox
items.
class myListboxItem
{
public Color ItemColor { get; set; }
public string Message { get; set; }
public myListboxItem(Color c, string m)
{
ItemColor = c;
Message = m;
}
}
The code to add the item to the ListBox
:
listBox1.Items.Add(new myListboxItem(Color.Red,"SKIPPED: " + partThreeOfPath));
This adds a item to the ListBox
, in black AddFoldersToClientFolder.myListboxItem
.
Upvotes: 1
Views: 332
Reputation: 236188
You can use DrawItem
event of ListBox:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
var item = (myListboxItem)listBox1.Items[e.Index];
e.DrawBackground();
using (var brush = new SolidBrush(item.ItemColor))
e.Graphics.DrawString(item.Message, listBox1.Font, brush, e.Bounds);
}
Note: you also need to set DrawMode
of ListBox to DrawMode.OwnerDrawFixed
or DrawMode.OwnerDrawVariable
Upvotes: 5