C# ListBox 项的背景颜色(winforms)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/91747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Background color of a ListBox item (winforms)
提问by Dested
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.
如何在 System.Windows.Forms.ListBox 中设置特定项目的背景颜色?如果可能,我希望能够设置多个。
采纳答案by Grad van Horck
Probably the only way to accomplish that is to draw the items yourself.
可能实现这一目标的唯一方法是自己绘制项目。
Set the DrawMode
to OwnerDrawFixed
设置DrawMode
为OwnerDrawFixed
and code something like this on the DrawItem event:
并在 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();
}
Second option would be using a ListView, although they have an other way of implementations (not really data bound, but more flexible in way of columns)
第二种选择是使用 ListView,虽然它们有另一种实现方式(不是真正的数据绑定,但在列的方式上更灵活)
回答by Matthew Scharley
// 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:
如果设置多个背景颜色的意思是为每个项目设置不同的背景颜色,那么这对于 ListBox 是不可能的,但是对于 ListView 来说是这样的,例如:
// Set the background of the first item in the list
MyListView.Items[0].BackColor = Color.Red;
回答by Shadow Wizard is Ear For You
Thanks for the answer by Grad van Horck, it guided me in the correct direction.
感谢Grad van Horck的回答,它引导我走向正确的方向。
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.
以上添加到给定的代码,将显示正确的文本并突出显示所选项目。
回答by Thulani Chivandikwa
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
完整样品
回答by Serdar Karaca
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();
}
}