列表框项目的颜色 [英] Color Of Listbox Items
本文介绍了列表框项目的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hey Guys,tnx for Help me。
我有一个列表框,它显示我的应用程序的日志,但其中一些日志
是错误所以我想当在我的列表框中添加一个错误,该项目的颜色(只有该项目)更改为另一种颜色,如红色.....
我使用Forecolor属性,但是它改变所有项目的颜色..
Hey Guys , tnx for Helping me.
I have a listbox , that shows me logs of my application but some of these logs
are Errors So I Wanna when add an Error to my Listbox , Color of that Item (only that Item) changed to another color like Red.....
I use Forecolor Property befor that but its change colors of all items ..
推荐答案
假设这是 Windows窗体ListBox
控件 [ ^ ],您需要设置DrawMode
[ ^ ]到OwnerDrawFixed
一个d处理DrawItem
事件 [ ^ ]。 MSDN有一个例子:
Assuming this is the Windows FormsListBox
control[^], you need to set theDrawMode
[^] toOwnerDrawFixed
and handle theDrawItem
event[^]. MSDN has an example:
private ListBox ListBox1 = new ListBox();
private void InitializeListBox()
{
ListBox1.Items.AddRange(new Object[]
{ "Red Item", "Orange Item", "Purple Item" });
ListBox1.Location = new System.Drawing.Point(81, 69);
ListBox1.Size = new System.Drawing.Size(120, 95);
ListBox1.DrawMode = DrawMode.OwnerDrawFixed;
ListBox1.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem);
Controls.Add(ListBox1);
}
private void ListBox1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
// Draw the background of the ListBox control for each item.
e.DrawBackground();
// Define the default color of the brush as black.
Brush myBrush = Brushes.Black;
// Determine the color of the brush to draw each item based
// on the index of the item to draw.
switch (e.Index)
{
case 0:
myBrush = Brushes.Red;
break;
case 1:
myBrush = Brushes.Orange;
break;
case 2:
myBrush = Brushes.Purple;
break;
}
// Draw the current item text based on the current Font
// and the custom brush settings.
e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(),
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
这篇关于列表框项目的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文