Format text in a RichTextBox in C#

[RichTextBox]

To format text in a RichTextBox, use the control’s Select method to select the text. Then use the “Selection” properties (SelectionAlignment, SelectionBackColor, SelectionFont, etc.) to set the properties for the selected text.

This example uses the following code to format three pieces of text in the first RichTextBox.

// Format RichTextBox1.
richTextBox1.Select(4, 5);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.Select(16, 3);
richTextBox1.SelectionFont =
    new Font(richTextBox1.Font, FontStyle.Italic);
richTextBox1.Select(35, 4);
richTextBox1.SelectionBackColor = Color.Yellow;
richTextBox1.SelectionColor = Color.Brown;
richTextBox1.Select(0, 0);

To make formatting a little easier, the following SelectRichText method selects a target string within a RichTextBox.

// Select the indicated text.
private void SelectRichText(RichTextBox rch, string target)
{
    int pos = rch.Text.IndexOf(target);
    if (pos < 0)
    {
        // Not found. Select nothing.
        rch.Select(0, 0);
    }
    else
    {
        // Found the text. Select it.
        rch.Select(pos, target.Length);
    }
}

The program uses this method to format text in its second RichTextBox as shown in the following code.

SelectRichText(richTextBox2, "quick");
richTextBox2.SelectionColor = Color.Red;


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in controls and tagged , , , , , , , , , , , . Bookmark the permalink.

5 Responses to Format text in a RichTextBox in C#

  1. jean says:

    hey thanks for this post, i just have a question : how to do the same using MVVM ??? thx a lot

    • RodStephens says:

      You could provide tools to set the selected text’s font properties: bold, italic, font name, font size, color, etc.

      In that case, you could store the text as RTF text in a string. That would be your model. The RichTextBox would be a view and a controller. You could also have a read-only RichTextBox that acts as a view only.

      Does that help? Or did you have something more specific in mind?

  2. bassem says:

    its very good code very helpful

  3. David says:

    The problem with your code is that if you have 2 of the same words in the richtextbox that you want bold or whatever, the code will always only select the first one. The second occurrence of the word will not be touched.

    • RodStephens says:

      Well, it’s true that only the first occurrence will be bold, but you need to decide whether that’s a problem. This example really just shows how to change the formatting for a piece of text. You need to decide which text you want to change, whether it be the first occurrence, last occurrence, or all occurrences.

      If you want to highlight all occurrences, you could do something like this:

      // Highlight all occurrences of the target text.
      private void HighlightRichText(RichTextBox rch, string target,
          Color bgcolor, Color fgcolor)
      {
          if ((target == null) || (target.Length == 0)) return;
          int pos = rch.Text.IndexOf(target);
          while (pos >= 0)
          {
              rch.Select(pos, target.Length);
              rch.SelectionBackColor = bgcolor;
              rch.SelectionColor = fgcolor;
              pos = rch.Text.IndexOf(target, pos + target.Length);
          }
      }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.