RichTextBox: Using TX Spell .NET with Multiple TX Text Control Instances

Integrating TX Spell .NET for Windows Forms into TX Text Control requires no code - it can be easily connected using the Visual Studio designer. To increase performance, the spelling dialog is opened by TX Text Control and not TX Spell .NET. The dialog that is opened by TX Text Control is pre-configured and can't be customized. Alternatively, you can call a fully customizable SpellCheckDialog from TX Spell .NET.

The source code is contained in the following directories:

  • Samples\WPF\CSharp\TextControl\Multiple controls
  • Samples\WPF\VB.NET\TextControl\Multiple controls

A high-performance spell checker requires a well-considered concept to work with multiple controls on a form in the same application. A single TX Spell .NET instance can be used with multiple instances of the RichTextBox. It is not necessary to have one instance of the spell checker, for each instance of the RichTextBox or any other control of the Framework or third-party control. This dramatically reduces the required memory and speeds up the spell checking process.

The concept of using multiple instances of the RichTextBox with the same instance of TX Spell .NET is very straightforward. It is not neccessary to switch manually between the controls. When text is changed in one of the RichTextBoxes, the WPF.Proofing.TXSpellChecker.Check method is called and the misspelled words are colorized:

TextRange range = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
int textPosition = GetTextPosition(currentPosition, richTextBox);
if (textPosition != -1)
    foreach (TXTextControl.Proofing.IncorrectWord incorrectWord in txSpellChecker1.IncorrectWords)
        if (incorrectWord.Start <= textPosition && textPosition <= incorrectWord.Start + incorrectWord.Length)
            ColorizeIncorrectWord(incorrectWord, richTextBox);

    TextRange word = GetWordAtPosition(textPosition, richTextBox);
    if (word != null)
        word.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
Dim range As New TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd)
Dim textPosition As Integer = GetTextPosition(currentPosition, richTextBox)
If textPosition <> -1 Then
    For Each incorrectWord As TXTextControl.Proofing.IncorrectWord In txSpellChecker1.IncorrectWords
        If incorrectWord.Start <= textPosition AndAlso textPosition <= incorrectWord.Start + incorrectWord.Length Then
            ColorizeIncorrectWord(incorrectWord, richTextBox)
        End If

    Dim word As TextRange = GetWordAtPosition(textPosition, richTextBox)
    If word IsNot Nothing Then
        word.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black)
    End If
End If

Start the application and type in text into each of the RichTextBoxes. Misspelled words are colorized in red automatically.