title: Color different texts of a RichTextBox string in C#
body: <p>I want to coloured different parts of a richtextbox with different colors, same as posted <a href="https://stackoverflow.com/questions/1926264/color-different-parts-of-a-richtextbox-string">here</a>.</p>
  
  <p>But with one difference, in my case I need to use the method Insert of the richtextbox to insert message at the top.</p>
  
  <p>I was using classical method:</p>
  
  <p>myRichTextBox.Text.Insert(0, myMessage);</p>
  
  <p>but using it I cannot coloured different parts of the message so I have done below extension methods:</p>
  
  <pre><code>public static class RichTextBoxExtensions
  {
      public static string Insert(this string str, int index, RichTextBox box, string text, Color color)
      {
          box.SelectionStart = box.TextLength;
          box.SelectionLength = 0;
  
          box.SelectionColor = color;
          //box.Text = str.Insert(0, box, text, color);
          box.Text = str.Insert(index, text);
          box.SelectionColor = box.ForeColor;
  
          return box.Text;
      }
  }
  </code></pre>
  
  <p>and then I use it by calling in the following way from any point of my program:</p>
  
  <pre><code>    private void PrintMessage(RichTextBox box, string message, Color color)
      {
          box.Text = box.Text.Insert(0, box, message, color);
      }
  </code></pre>
  
  <p>But it is not working, it throws an exception in extension method Insert in line:</p>
  
  <pre><code>box.Text = str.Insert(0, box, text, color);
  </code></pre>
  
  <p>It says "Stack overflow". Any ideas?</p>
  
  <p><strong>UPDATED</strong>:</p>
  
  <pre><code>    private void PrintMessage(RichTextBox box, string message, Color color)
      {
          box.SelectedText = box.SelectedText.Insert(0, box, message, color);
      }
  
  public static class RichTextBoxExtensions
  {
      public static string Insert(this string str, int index, RichTextBox box, string text, Color color)
      {
          box.SelectionStart = box.TextLength;
          box.SelectionLength = 0;
  
          box.SelectionColor = color;
          box.SelectedText = str.Insert(index, text);
          box.SelectionColor = box.ForeColor;
  
          return box.Text;
      }
  }
  </code></pre>
  
  <p>But it does not work, text does not appear coloured.</p>
  
  <p><strong>NOTE</strong>:</p>
  
  <p>I need text to be added at the top of the richtextbox control, not at the bottom, so I was using Insert instead of AppendText.</p>
  
  <p><strong>UPDATE 2</strong>:
  Using AppendText and setting SelectionStart to 0 to append text at the top of the richtextbox as TaW says <a href="https://stackoverflow.com/questions/38097850/richtextbox-prepend-new-text-with-color">here</a>, fails the first time you print a message into the richtextbox. Rest of time (not the first) is going ok.</p>
  
  <p>So how to make it work also for the first string appended into the richtextbox?</p>
  
  <pre><code>    public static void AppendText(this RichTextBox rtb, string text, Color color)
      {            
          rtb.SelectionStart = 0;            // set the cursor to the target position
          rtb.SelectionLength = 0;           // nothing selected, yet
          rtb.SelectedText = text;           // this inserts the new text 
          rtb.SelectionLength = text.Length; // now we prepare the new formatting
          rtb.SelectionColor = color;
      }
  </code></pre>