I have this class:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Windows.Forms; namespace GatherLinks { class ColorText { public static void Texts(RichTextBox box, string text, Color color) { box.SelectionStart = box.TextLength; box.SelectionLength = 0; box.SelectionColor = color; box.AppendText(text); box.SelectionColor = box.ForeColor; } public static void ColorListBox(List<string> data, DrawItemEventArgs e) { int keywords = 0; string keyword = null; string url = data[e.Index].Substring(0, 5); if (data[e.Index].Contains("Local KeyWord:")) { keywords = data[e.Index].IndexOf("Local KeyWord:"); keyword = data[e.Index].Substring(keywords, 14); } else { keywords = data[e.Index].IndexOf("Localy KeyWord:"); keyword = data[e.Index].Substring(keywords, 15); } using (Font f = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular)) { if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) e.Graphics.FillRectangle(Brushes.LightBlue, e.Bounds); else { using (SolidBrush sb = new SolidBrush(SystemColors.Window)) e.Graphics.FillRectangle(sb, e.Bounds); } SizeF size = e.Graphics.MeasureString(url, f); using (SolidBrush sb = new SolidBrush(Color.Red)) { e.Graphics.DrawString(url, f, sb, new PointF(e.Bounds.X, e.Bounds.Y)); string toMeasure = data[e.Index].Substring(0, keywords - 1); float startPos = e.Graphics.MeasureString(toMeasure, f).Width; e.Graphics.DrawString(keyword, f, sb, new PointF(e.Bounds.X + (int)startPos, e.Bounds.Y)); } // string token = data[e.Index].Substring(url.Length, data[e.Index].LastIndexOf(" --- ") - (url.Length)); // e.Graphics.DrawString(token, f, Brushes.Black, new PointF(e.Bounds.X + size.Width, e.Bounds.Y)); // Get the string to print in black. int first = url.Length; int last = data[e.Index].LastIndexOf(" --- ") + 5; //(5 = length of " --- "). int length = last - first; string token = data[e.Index].Substring(first, length); // Get the place to draw it. size = e.Graphics.MeasureString(url, f); float positionX = size.Width; // Draw the string. e.Graphics.DrawString(token, f, Brushes.Black, new PointF(positionX, e.Bounds.Y)); //size = e.Graphics.MeasureString(url + token, f); //size = e.Graphics.MeasureString("Url:" + token, f); //e.Graphics.DrawString(data[e.Index].Substring(data[e.Index].IndexOf(token) + token.Length, data[e.Index].LastIndexOf(": ") - token.Length - data[e.Index].IndexOf(token) + 1), f, Brushes.Black, new PointF(size.Width, e.Bounds.Y)); token = data[e.Index].Substring(data[e.Index].LastIndexOf(": ") + 2); size = e.Graphics.MeasureString(data[e.Index].Substring(0, data[e.Index].LastIndexOf(token)), f); using (SolidBrush sb = new SolidBrush(Color.Green)) e.Graphics.DrawString(token, f, sb, new PointF(e.Bounds.X + size.Width + 4, e.Bounds.Y)); e.DrawFocusRectangle(); } } } }
But now in Form1 i have this ( for testing ):
data.Add("Gpu Temeprature --- " + form1_location_on_x); data.Add("Cpu Temperature --- " + form1_location_on_y); listBox1.DataSource = data;
I have in Form1 also this listBox1 event:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index == -1) { } else { string url = data[e.Index].Substring(0, 5); using (Font f = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular)) { ColorText.ColorListBox(data, e); } } }
And this event also in Form1:
private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e) { Size textSize = e.Graphics.MeasureString(data[e.Index].ToString(), this.Font).ToSize(); e.ItemHeight = textSize.Height; e.ItemWidth = textSize.Width; if (e.ItemWidth > listBox1.HorizontalExtent) listBox1.HorizontalExtent = e.ItemWidth; }
In general i want to color each item in the listBox the first text in Red the symbol "---" in Black and the value in the end in Green. Same colors for each item in the listBox.
How can I do it and what should I change in the class and in the function ColorListBox ?