I am interested in only free solutions, and preferred that they are not heavy to implement or change current usage (the least effort to introduce colored ListBox in place of normal one the better).
With regards,
MadBoy
This article tells how to use DrawItem of a ListBox with DrawMode set to one of the OwnerDraw values. Basically, you do something like this:
listBox1.DrawMode = OwnerDrawFixed;
listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
// TODO: Split listBox1.Items[e.Index].ToString() and then draw each separately in a different color
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold),new SolidBrush(color[e.Index]),e.Bounds);
}
Instead of the single DrawString call, Split listBox1.Items[e.Index].ToString() into words and make a separate call to DrawString for each word. You'll have to replace e.bounds with an x,y location or a bounding rectangle for each word.
The same approach should work for ListView.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With