C# 列表框项目的背景色(winforms)
如何设置System.Windows.Forms.ListBox中特定项目的背景色?如果可能的话,我希望能够设置多个项目。实现这一点的唯一方法可能是自己绘制项目 将C# 列表框项目的背景色(winforms),c#,winforms,listbox,colors,C#,Winforms,Listbox,Colors,如何设置System.Windows.Forms.ListBox中特定项目的背景色?如果可能的话,我希望能够设置多个项目。实现这一点的唯一方法可能是自己绘制项目 将DrawMode设置为OwnerDrawFixed 并在DrawItem事件上编写如下代码: private void listBox_DrawItem(object sender, DrawItemEventArgs e) { e.DrawBackground(); Graphics g = e.Graphics;
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,尽管它们有另一种实现方式(不是真正的数据绑定,而是更灵活的列方式)
如果设置多个背景色的意思是为每个项目设置不同的背景色,则在ListBox中不可能这样做,但在ListView中可以这样做,例如:
// Set the background of the first item in the list
MyListView.Items[0].BackColor = Color.Red;
谢谢你的帮助,它指引了我正确的方向
为了支持文本(不仅仅是背景色),以下是我的完整工作代码:
//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();
}
//具有普通/选定颜色的全局笔刷
private SolidBrush reportsForegroundBrushSelected=新的SolidBrush(颜色为白色);
private SolidBrush reportsforgroundbrush=新的SolidBrush(Color.Black);
私有SolidBrush reportsBackgroundBrushSelected=新的SolidBrush(Color.FromKnownColor(KnownColor.Highlight));
私有SolidBrush reportsBackgroundBrush1=新的SolidBrush(颜色为白色);
私有SolidBrush reportsBackgroundBrush2=新的SolidBrush(颜色为灰色);
//要绘制项目,请不要忘记将ListBox的DrawMode设置为OwnerDrawFixed
私有void lbReports\u DrawItem(对象发送方,DrawItemEventArgs e)
{
e、 牵引杆接地();
bool selected=((e.State&DrawItemState.selected)=DrawItemState.selected);
int-index=e.index;
如果(索引>=0&&index SolidBrush foregroundBrush=(选定)?reportsForegroundBrushSelected:reportsForegroundBrush;
g、 DrawString(文本,例如字体,前置笔刷,lbReports.GetItemRectangle(index.Location));
}
e、 DrawFocusRectangle();
}
上述内容将添加到给定代码中,并将显示正确的文本以及高亮显示所选项目
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();
}
完整样本可以使用列表框。请参阅s/可能/容易/。哦,好吧。C#1,新手0。我以前没有使用过太多重载绘制方法。背景色不是列表框的属性。ObjectCollectionItem非常好,所选位非常有用。什么是ReportsForgroundBrushSelected:ReportsForgroundBrushSelected??ReportsForgroundBrushSelected:ReportsForgroundBrush给我错误,它们应该被宣布,但如何宣布?@PrakashVishwakarma完成,请参阅我的编辑。感谢大家的提醒,我还提高了整体效率,每次调用错误的方法,我都会创建新的笔刷实例。
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();
}
}
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();
}
}