using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Extensions { public static class RichTextBoxExtensions { public static void AppendText(this RichTextBox box, string text, Color color) { if (box.InvokeRequired) { box.Invoke((MethodInvoker)(delegate { box.SelectionStart = box.TextLength; box.SelectionLength = 0; box.SelectionColor = color; box.AppendText(text); box.SelectionColor = box.ForeColor; box.ScrollToCaret(); })); } else { box.SelectionStart = box.TextLength; box.SelectionLength = 0; box.SelectionColor = color; box.AppendText(text); box.SelectionColor = box.ForeColor; box.ScrollToCaret(); } } public static void AppendText(this RichTextBox box, string text, Color bgcolor, Color fgcolor) { if (box.InvokeRequired) { box.Invoke((MethodInvoker)(delegate { box.SelectionStart = box.TextLength; box.SelectionLength = 0; box.SelectionColor = fgcolor; box.SelectionBackColor = bgcolor; box.AppendText(text); box.SelectionColor = box.ForeColor; box.SelectionBackColor = box.BackColor; box.ScrollToCaret(); })); } else { box.SelectionStart = box.TextLength; box.SelectionLength = 0; box.SelectionColor = fgcolor; box.SelectionBackColor = bgcolor; box.AppendText(text); box.SelectionColor = box.ForeColor; box.SelectionBackColor = box.BackColor; box.ScrollToCaret(); } } } }