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;




hey thanks for this post, i just have a question : how to do the same using MVVM ??? thx a lot
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?
its very good code very helpful
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.
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: