Append Text to RichTextBox with color - CSharp System.Windows.Forms

CSharp examples for System.Windows.Forms:RichTextBox

Description

Append Text to RichTextBox with color

Demo Code



public class Main{
        //for RichTextBox
        public static void AppendText(this System.Windows.Forms.RichTextBox rtb, string text, System.Drawing.Color color)
        {//from   www .  j  a v  a 2  s  . c o  m
            int start = rtb.TextLength;
            rtb.AppendText(text);
            
            int end = rtb.TextLength;

            // Textbox may transform chars, so (end-start) != text.Length
            rtb.Select(start, end - start);
            {
                rtb.SelectionColor = color;
                // could set box.SelectionBackColor, box.SelectionFont too.
            }
            rtb.SelectionLength = 0; // clear
        }
}

Related Tutorials