//Get the last cursor position in the richTextBox1.
int selPos = richTextBox1.SelectionStart;
//For each match from the Regex, highlight the word.
foreach (Match keyWordMatch in varNameHere.Matches(richTextBox1.Text))
{
richTextBox1.Select(keyWordMatch.Index, keyWordMatch.Length);
//The color of the highlighted text is Blue.
richTextBox1.SelectionColor = Color.Blue;
//This just keeps the program from highlighting the words after the one you want, from being highlighted.
richTextBox1.SelectionStart = selPos;
richTextBox1.SelectionColor = Color.Black;
}
public partial class mainForm : Form
{
public Regex varNameHere = new Regex("words|to|be|highlighted|in|this|color");
//Get the last cursor position in the richTextBox1.
int selPos = richTextBox1.SelectionStart;
//For each match from the Regex, highlight the word.
foreach (Match keyWordMatch in varNameHere.Matches(richTextBox1.Text))
{
richTextBox1.Select(keyWordMatch.Index, keyWordMatch.Length);
//The color of the highlighted text is Blue.
richTextBox1.SelectionColor = Color.Blue;
//This just keeps the program from highlighting the words after the one you want, from being highlighted.
richTextBox1.SelectionStart = selPos;
richTextBox1.SelectionColor = Color.Black;
}
public partial class mainForm : Form
{
public Regex varNameHere = new Regex("words|to|be|highlighted|in|this|color");
Copyright © 2025, NextGenUpdate.
All Rights Reserved.