title: How to highliht specific word in richtextbox during writing?
body: <p>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? </p>
  
  <p>Here is the timer tick void:</p>
  
  <pre><code>void CheckTimer_Tick(object sender, EventArgs e)
      {
          List&lt;string&gt; correct_words = File.ReadAllLines(DictPath).ToList();
          string text = MainText.Text;
          string[] Words = text.Split(' ', '.');
  
          for (int i = 0; i &lt; Words.Length; i++)
          {
              if (correct_words.Contains(Words[i])) { }
              else
              {
                  int index = 0;
                  String temp = MainText.Text;
                  MainText.Text = "";
                  MainText.Text = temp;
  
                  while (index &lt; 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;
                  }
  
              }
          }
  
      }
  </code></pre>
  
  <p>I also tried this void:</p>
  
  <pre><code>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 &lt; 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;
      }
  </code></pre>
  
  <p>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.</p>