title: How to highlight specific text in a RichTextBox?
body: <p>In a <code>RichTextBox</code> control, I am trying to highlight a specific text in RED color as a result from a Regex match. </p>
  
  <p>Sample:</p>
  
  <p>This text statement works fine and highlights the 'Deans' in red shade:</p>
  
  <blockquote>
    <p>select top 10 * from client where MailName='Deans'</p>
  </blockquote>
  
  <p>This text statement works fine also and highlights the code in red shade:</p>
  
  <blockquote>
    <p>select top 10 * from client where MailName='De a ns'</p>
  </blockquote>
  
  <p>But this statement fails:</p>
  
  <blockquote>
    <p>select top 10 * from client where MailName='Deans '</p>
  </blockquote>
  
  <p>It fails because there is space in between the single quotes, it throws an exception on the line making the start TextRange at </p>
  
  <pre><code>TextRange result = new TextRange(start, start.GetPositionAtOffset(word.Length)); 
  </code></pre>
  
  <p>The exception says:</p>
  
  <blockquote>
    <p>invalid parameter position 2.</p>
  </blockquote>
  
  <p>Please suggest me a solution. Where am I making a mistake?</p>
  
  <p>Here is the code snippet:</p>
  
  <pre><code>string StringFromRichTextBox(RichTextBox rtb)
  {
      TextRange textRange = new TextRange(rtb.Document.ContentStart,      
                  rtb.Document.ContentEnd);
  
      return textRange.Text;
  }
  
  private void TextChangedEventHandler(object sender, TextChangedEventArgs e)
  {
      TextRange documentRange = new TextRange(txtQueries.Document.ContentStart, txtQueries.Document.ContentEnd);
          documentRange.ClearAllProperties();
  
      MatchCollection match = Regex.Matches(StringFromRichTextBox(txtQueries), @"'([^']*)'");
      for (int z = 0; z &lt; match.Count; z++)
      {
          CheckKeyword(documentRange, match[z].ToString().Trim());
      }
  }
  
  private void CheckKeyword(TextRange textRange, string word)
  {
      //txtQueries.TextChanged -= this.TextChangedEventHandler;
      if (textRange.Text.Contains(word))
      {
          int offset = textRange.Text.IndexOf(word);
          if (offset &lt; 0)
          {
              return;
          }// Not found
          else
          {
              // Try to select the text as a contiguous range
              for (TextPointer start = textRange.Start.GetPositionAtOffset(offset); start != textRange.End; start = start.GetPositionAtOffset(1))
              {
                  try
                  {
                      TextRange result = new TextRange(start, start.GetPositionAtOffset(word.Length));
                      if (result != null &amp;&amp; result.Text == word)  
                      {
                          result.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Red));
                          break;
                      }
                  }
                  catch(Exception ex)
                  {
                      throw new Exception(ex.Message);
                  }
              }
          }
      }
      //txtQueries.TextChanged += this.TextChangedEventHandler;
  }
  </code></pre>
  
  <p>Error Message:</p>
  
  <blockquote>
    <p>Value cannot be null.
    Parameter name: position2
    at System.Windows.Documents.TextRange..ctor(TextPointer position1, TextPointer position2)</p>
  </blockquote>