如何在System.Windows.Forms.ListBox中设置特定项目的背景颜色?如果可能的话,我希望能够设置多个。
答案 0 :(得分:55)
感谢answer by Grad van Horck,它引导我朝着正确的方向前进。
为了支持文本(不仅仅是背景颜色),这是我完全正常工作的代码:
//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();
}
以上内容会添加给定代码并显示正确的文字以及突出显示所选项目。
答案 1 :(得分:51)
实现这一目标的唯一方法可能是自己绘制物品。
将DrawMode
设置为OwnerDrawFixed
并在DrawItem事件上编写类似的代码:
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();
}
第二个选项是使用ListView,虽然它们有另一种实现方式(不是真正的数据绑定,但是列的方式更灵活)
答案 2 :(得分:2)
// 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;
如果你设置多种背景颜色的意思是为每个项目设置不同的背景颜色,这对于ListBox是不可能的,但是对于带有ListView的IS是不可能的,例如:
// Set the background of the first item in the list
MyListView.Items[0].BackColor = Color.Red;
答案 3 :(得分:0)
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();
}
完整样本
答案 4 :(得分:0)
public MainForm()
{
InitializeComponent();
this.listbox1.DrawItem += new DrawItemEventHandler(this.listbox1_DrawItem);
}
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();
}
}