• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 637
  • Last Modified:

How to Highlight Text Within a RichTextBox Using C# .NET WIndows Forms

I am attempting to write a RichTextBox and I need to be able to write part of a sentence highlighted. or change the color of the text of a specific part of a string, and the remainder of the text in the sentence in regular type. Could someone help me figure out what the syntax would be in order to complete this task? For instance, if the control was called RichTextBox1 and the sentence was "The dog crossed the road", how would I get the word "dog" to be highlighted, or in a different font color, and the remainder of the sentence to be regular type?

Any help that could be provided on this topic would be greatly appreciated.
2 Solutions
käµfm³d 👽Commented:
Something like this should do it:

string find = "dog";
int start = RichTextBox1.Text.IndexOf(find);

RicthTextBox1.SelectionStart = start;
RicthTextBox1.SelectionLength = find.Length;
RicthTextBox1.SelectionColor = Color.Red;

Open in new window

Anil GolamariCommented:

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Tackle projects and never again get stuck behind a technical roadblock.
Join Now