I have this class i did that color the text in a specific color:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Windows.Forms; namespace Irc_Bot { 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(); } } } }
Then i use it in Form1 for example like this:
this.Invoke(new MethodInvoker(delegate { label10.Text = e.failedUrls.ToString(); })); this.Invoke(new MethodInvoker(delegate { ColorText.Texts(richTextBox1, " Failed " + Environment.NewLine,
Color.Green); }));
so i have a label lets say label10 is in Red and the text near it will be in Green.
But i want somehow to make that i will not have to use labels in the richTextBox
Somehow ot make that the class ColorText.Texts will be able to make something like:
ColorText.Texts(richTextBox1,"Im red",Color.Red,"Im green",Color.Green);
So the output will be in the same line in the richTextBox "Im red Im green"
The Im red will be colored in Red and the green in Green.
Is there any way to do it ?