Jan*_*lem 1 c# listbox winforms
我想改变一个ListBox 项目的颜色.我的代码似乎不起作用.它只是将类的名称空间添加到ListBox项目中.
class myListboxItem
{
public Color ItemColor { get; set; }
public string Message { get; set; }
public myListboxItem(Color c, string m)
{
ItemColor = c;
Message = m;
}
}
Run Code Online (Sandbox Code Playgroud)
将项添加到的代码ListBox:
listBox1.Items.Add(new myListboxItem(Color.Red,"SKIPPED: " + partThreeOfPath));
Run Code Online (Sandbox Code Playgroud)
这会ListBox在黑色中添加一个项目AddFoldersToClientFolder.myListboxItem.
您可以使用DrawItemListBox的事件:
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);
}
Run Code Online (Sandbox Code Playgroud)
注意:您还需要将DrawModeListBox 设置为DrawMode.OwnerDrawFixed或DrawMode.OwnerDrawVariable