Basically I want to make an autocorrect for my language. I have a RichTextBox(name: MainText), where I write. during writing, the program should every word if it exists in a dictionary file. if not then change the specific word color to red. 
It has a timer. after every second it gets the written text and puts the words to an str array, and reads correct words from dictionary.txt file and puts them in a list. during comparison of the strings, it never highlights the incorrect words and it has always indexOutOfRange errors. How to fix it? 

Here is the timer tick void:

```
void CheckTimer_Tick(object sender, EventArgs e)
    {
        List<string> correct_words = File.ReadAllLines(DictPath).ToList();
        string text = MainText.Text;
        string[] Words = text.Split(' ', '.');

        for (int i = 0; i < Words.Length; i++)
        {
            if (correct_words.Contains(Words[i])) { }
            else
            {
                int index = 0;
                String temp = MainText.Text;
                MainText.Text = "";
                MainText.Text = temp;

                while (index < MainText.Text.LastIndexOf(Words[i]))
                {
                    MainText.Find(Words[i], index, MainText.TextLength, RichTextBoxFinds.None);
                    MainText.SelectionColor = Color.Red;
                    index = MainText.Text.IndexOf(Words[i], index) + 1;
                }

            }
        }

    }

```

I also tried this void:

```
void HighlightPhrase(RichTextBox box, string phrase, Color color)
    {
        int pos = box.SelectionStart;
        string s = box.Text;

        for (int ix = 0; ;)
        {
            int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase);
            if (jx < 0) break;
            box.SelectionStart = jx;
            box.SelectionLength = phrase.Length;
            box.SelectionColor = color;
            ix = jx + 1;
        }
        box.SelectionStart = pos;
        box.SelectionLength = 0;
        box.ForeColor = Color.Black;
    }

```

This worked if I gave it a specific string, but it couldn't recognize the phrase input from the dictionary and gave the indexOutOfRange error.