Answered by:
Add items in ListBox with different colors

Question
-
How to add items with different color in ListBox control?This code for example change the colors of all items in the ListBox.
Obviously i do not want that.
Code Blockthis.listBox1.DrawItem += new DrawItemEventHandler(this.DrawItemHandler);
private void DrawItemHandler(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
e.Graphics.DrawString(someString,
new Font(FontFamily.GenericSansSerif,
8, FontStyle.Bold),
new SolidBrush(tColor),e.Bounds);
}Wednesday, December 5, 2007 9:57 AM
Answers
-
Hi cygnusx,
To set different color for each item in the ListBox, please check the following sample.
Code Blocknamespace ListBox
{
public partial class ColoredLB : Form
{
public ColoredLB()
{
InitializeComponent();
}
private void ColoredLB_Load(object sender, EventArgs e)
{
this.listBox1.Items.AddRange(new object[] { "Red", "Blue", "Yellow","Other Color" });
this.listBox1.DrawMode = DrawMode.OwnerDrawVariable;
this.listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);
}
void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
Color c = Color.Pink;
if (this.listBox1.Items[e.Index].ToString() == "Red")
c = Color.Red;
else if (this.listBox1.Items[e.Index].ToString() == "Blue")
c = Color.Blue;
else if (this.listBox1.Items[e.Index].ToString() == "Yellow")
c = Color.Yellow;
e.Graphics.DrawString(this.listBox1.Items[e.Index].ToString(), new Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold), new SolidBrush(c), e.Bounds);
}
}
}
Hope this helps.
Best regards.
Rong-Chun ZhangTuesday, December 11, 2007 4:21 AM
All replies
-
Hi cygnusx,
To set different color for each item in the ListBox, please check the following sample.
Code Blocknamespace ListBox
{
public partial class ColoredLB : Form
{
public ColoredLB()
{
InitializeComponent();
}
private void ColoredLB_Load(object sender, EventArgs e)
{
this.listBox1.Items.AddRange(new object[] { "Red", "Blue", "Yellow","Other Color" });
this.listBox1.DrawMode = DrawMode.OwnerDrawVariable;
this.listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);
}
void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
Color c = Color.Pink;
if (this.listBox1.Items[e.Index].ToString() == "Red")
c = Color.Red;
else if (this.listBox1.Items[e.Index].ToString() == "Blue")
c = Color.Blue;
else if (this.listBox1.Items[e.Index].ToString() == "Yellow")
c = Color.Yellow;
e.Graphics.DrawString(this.listBox1.Items[e.Index].ToString(), new Font(FontFamily.GenericSansSerif, 8, FontStyle.Bold), new SolidBrush(c), e.Bounds);
}
}
}
Hope this helps.
Best regards.
Rong-Chun ZhangTuesday, December 11, 2007 4:21 AM -
I am trying this code below, and it's obviously not doing what I want.I have set the drawmode on formload.I am adding one item at a time, not add range. But I don't think this should make a difference.Any ideas?
private void lbChanUsers_DrawItem(object sender, DrawItemEventArgs e){e.DrawBackground();e.DrawFocusRectangle();Color c = Color.Black;if (this.lbChanUsers.Items[e.Index].ToString().IndexOf("@") < 0)return;if (this.lbChanUsers.Items[e.Index].ToString().IndexOf("@") >= 0)c = Color.Blue;if (this.lbChanUsers.Items[e.Index].ToString().IndexOf("+") >= 0)c = Color.Maroon;this.lbChanUsers.Items[e.Index] = (string)this.lbChanUsers.Items[e.Index].ToString().Substring(1);e.Graphics.DrawString(this.lbChanUsers.Items[e.Index].ToString(), lbChanUsers.Font, new SolidBrush(c), e.Bounds);}
foxjazzWednesday, December 30, 2009 10:25 PM