The coloring is not so hard but defining the term 'word' is not so simple..
Here is an example for both:
I assume that you can live with the Regex definition of word boundaries. You may need to improve on the wordPattern
depending on your needs, i.e. on your grammar! (See here for an example!)
int ColorWord(RichTextBox rtb, string word, Color foreColor, Color backColor)
{
string wordPattern = @"\b" + word + @"\b";
var matches = Regex.Matches(rtb.Text, wordPattern);
for (int i = matches.Count - 1; i >= 0; i--)
{
var m = matches[i];
rtb.SelectionStart = m.Index;
rtb.SelectionLength = m.Length;
rtb.SelectionColor = foreColor;
rtb.SelectionBackColor = backColor;
}
return matches.Count;
}
This function will set fore- and backcolor for each occurance of a given word in a RichTextBox. It transverses the matches from the back just for good measure as we don't really modify any text, so the letters won't shift; so we could have looped from start to end as well; but maybe you want to adapt it one day for changing words..
Once you understand how coloring (or formatting in any other way) the RTB text works, i.e. by first selecting a portion of the text and then changing the RTB.SelectedXXX
property, it should be simple to modify..
Note that the word boundaries are meant to work with normal text. It is up to you to define maybe new rules to include or exclude characters for the language you want to highlight..