Use this answer as a reference for the results:
How to color different words with different colors in a RichTextBox
This class object is used keep track of the words to color and the related color to use when one of these words is written or changed:
(The linked answer explains how to fill the list with words and related colors)
public class ColoredWord
{
public string Word { get; set; }
public Color WordColor { get; set; }
}
public List<ColoredWord> ColoredWords = new List<ColoredWord>();
In the RichTextBox
KeyUp()
event, check whether a word in the list is being inserted or modified:
Here I'm using the KeyUp()
event because at the moment it's raised, the word has already been inserted/modified.
private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
{
if ((e.KeyCode >= Keys.Left & e.KeyCode <= Keys.Down)) return;
if (e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.Alt || e.KeyCode == Keys.ControlKey) return;
int CurrentPosition = richTextBox1.SelectionStart;
int[] WordStartEnd;
string word = GetWordFromPosition(richTextBox1, CurrentPosition - 1, out WordStartEnd);
ColoredWord result = ColoredWords.FirstOrDefault(s => s.Word == word.ToUpper());
SetSelectionColor(richTextBox1, result, CurrentPosition, WordStartEnd);
if (e.KeyCode == Keys.Space && result == null)
{
word = GetWordFromPosition(richTextBox1, CurrentPosition + 1, out WordStartEnd);
result = ColoredWords.FirstOrDefault(s => s.Word == word.ToUpper());
SetSelectionColor(richTextBox1, result, CurrentPosition, WordStartEnd);
}
}
These are the helpers methods that are used to color a Word when it's included in the list and to extract the word from the current caret position.
The case when an already colored word is split by a space char, thus both ends lose their color, is handled in the KeyUp()
event (the code part is: if (e.KeyCode == Keys.Space && result == null
).
private void SetSelectionColor(RichTextBox ctl, ColoredWord word, int Position, int[] WordStartEnd)
{
ctl.Select(WordStartEnd[0], WordStartEnd[1]);
if (word != null)
{
if (ctl.SelectionColor != word.WordColor)
ctl.SelectionColor = word.WordColor;
}
else
{
if (ctl.SelectionColor != ctl.ForeColor)
ctl.SelectionColor = ctl.ForeColor;
}
ctl.SelectionStart = Position;
ctl.SelectionLength = 0;
ctl.SelectionColor = richTextBox1.ForeColor;
}
private string GetWordFromPosition(RichTextBox ctl, int Position, out int[] WordStartEnd)
{
int[] StartEnd = new int[2];
StartEnd[0] = ctl.Text.LastIndexOf((char)Keys.Space, Position - 1) + 1;
StartEnd[1] = ctl.Text.IndexOf((char)Keys.Space, Position);
if (StartEnd[1] == -1) StartEnd[1] = ctl.Text.Length;
StartEnd[1] -= StartEnd[0];
WordStartEnd = StartEnd;
return ctl.Text.Substring(StartEnd[0], StartEnd[1]);
}