Getting ready to traverse the Text contents of the RichTextBox word-by-word:
private Color HighLightForeColor = Color.Yellow;
private Color HighLightBackColor = Color.Navy;
private Color previousSelectionForeColor;
private Color previousSelectionBackColor;
private string theText;
private List<string> theWords = new List<string>();
private List<point> theWordsLocations = new List<point>();
private char[] splitChar = new[] {' ', '\n'};
private int nWords = 0;
private int currentWord = 0;
private Point previousSelection;
private void Form1_Load(object sender, EventArgs e)
{
theWords = richTextBox1.Text.Split(splitChar, StringSplitOptions.RemoveEmptyEntries).ToList();
nWords = theWords.Count;
int location = 0;
foreach (string word in theWords)
{
theWordsLocations.Add(new Point(location, word.Length));
location += word.Length + 1;
}
}
We just built two Lists, one to hold each word in the Text, another to hold the location, and length, of the word in the Text. So, the word at position #5 in the list of words will have the location of the Point in position #5 of the list of word locations. Because of the possibility of duplicate words, we can't use a generic Dictionary here.
A Point is used here, for convenience, to hold the two integers that describe a word's start location and length; a "cheap" way to hold two integers that's easier to use than a Tuple, or other structure.
Possible future problems:
1. other kinds of white-space in the Text ? Tabs ?
2. right now no attempt to deal with punctuation characters immediately after a word.
3. what about character encoding, or other possible "culture" factors here: can you assume they're always the same ?