RichTextBox: Using a Customizable SpellCheckDialog

TX Spell .NET enjoys a deep integration within the main word processing component. 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. But TX Spell .NET can be used with all kind of .NET Framework or third-party controls. This sample shows how to use TX Spell .NET with the .NET RichTextBox and customizable SpellCheckDialog.

The source code is contained in the following directories:

  • Samples\WPF\CSharp\RichTextBox\SpellCheckDialog
  • Samples\WPF\VB.NET\RichTextBox\SpellCheckDialog

Principally, the integrated SpellCheckDialog of TX Spell .NET can be used with all types of input controls. If used in combination with TX Text Control, the handling is completely automated and no additional code is required. In order to use TX Spell .NET with any other control, the WPF.Proofing.TXSpellChecker.Check method must be called before the dialog is opened:

txSpellChecker1.Check(new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text);
txSpellChecker1.SpellCheckDialog.Show(this);
txSpellChecker1.Check(New TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text)
txSpellChecker1.SpellCheckDialog.Show(Me)

The dialog is automatically opened with the text of the RichTextBox. In the WPF.Proofing.TXSpellChecker.IncorrectWordChanging> event, the misspelled word must be replaced in the connected control:

void txSpellChecker1_IncorrectWordChanging(object sender, TXTextControl.Proofing.IncorrectWordChangingEventArgs e)
{
    if (e.Type != TXTextControl.Proofing.TXSpell.ChangeType.Ignore)
    {
        TextRange range = GetTextRangeInDocument(e.IncorrectWord.Start, e.IncorrectWord.Start + e.IncorrectWord.Length, new
TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd));

        if (e.Type == TXTextControl.Proofing.TXSpell.ChangeType.Replace)
        {
            range.Text = e.CorrectedWord;
        }
        else
        {
            if (e.Type == TXTextControl.Proofing.TXSpell.ChangeType.Delete)
            {
                range.Text = "";
            }
        }
    }
}
Private Sub txSpellChecker1_IncorrectWordChanging(sender As Object, e As TXTextControl.Proofing.IncorrectWordChangingEventArgs)
    If e.Type <> TXTextControl.Proofing.TXSpell.ChangeType.Ignore Then
        Dim range As TextRange = GetTextRangeInDocument(e.IncorrectWord.Start, e.IncorrectWord.Start + e.IncorrectWord.Length, New
TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd))

        If e.Type = TXTextControl.Proofing.TXSpell.ChangeType.Replace Then
            range.Text = e.CorrectedWord
        Else
            If e.Type = TXTextControl.Proofing.TXSpell.ChangeType.Delete Then
                range.Text = ""
            End If
        End If
    End If
End Sub

Start the sample application and click on Spell Check Dialog to start the checking process. The corrected words are replaced in the connected RichTextBox through the attached IncorrectWordChanging event.

Image