TX Spell .NET for WPF

Open the Spell Check Dialog

Topic

The spell check dialog of TX Spell .NET works with any control, not only with TX Text Control. For a RichTextBox, the dialog walks through the misspelled words and the user picks a correction, while a short event handler writes each correction back into the document. You add this to the application of Check Spelling as You Type.

Prerequisites

  • The application of Check Spelling as You Type: a window with a RichTextBox called richTextBox1, a TXSpellChecker resource with the key txSpellChecker1, and the methods GetRange and GetTextPointerAtOffset, in a project with the package TXTextControl.TXSpell.WPF.SDK.

How the Dialog Works With a RichTextBox

A RichTextBox knows nothing about the spell checker, so the work is split in two:

  1. Your code passes the text of the document to Check. The spell checker finds the incorrect words and keeps them in IncorrectWords.
  2. Show opens the dialog with these words. Each time the user changes or deletes a word, the spell checker raises IncorrectWordChanging, and your handler makes the same change in the document.

Add the Button

In MainWindow.xaml, add a Button to the DockPanel, before the RichTextBox, so that it is docked at the bottom and the RichTextBox fills the rest:

<DockPanel>
    <Button x:Name="buttonSpelling" DockPanel.Dock="Bottom" Content="Spell Check Dialog..."
            Click="buttonSpelling_Click" />
    <RichTextBox x:Name="richTextBox1" TextChanged="richTextBox1_TextChanged" />
</DockPanel>

Write the Code

  1. In the constructor of MainWindow.xaml.cs, attach the handler after FindResource:

    txSpellChecker1.IncorrectWordChanging += txSpellChecker1_IncorrectWordChanging;
    
  2. Add the handler of the button and the handler of the event to the class:

    private void buttonSpelling_Click(object sender, RoutedEventArgs e)
    {
        FlowDocument document = richTextBox1.Document;
    
        // Find the incorrect words, then walk through them in the dialog
        txSpellChecker1.Check(new TextRange(document.ContentStart, document.ContentEnd).Text);
        txSpellChecker1.SpellCheckDialog.Show(this);
    }
    
    private void txSpellChecker1_IncorrectWordChanging(object sender, IncorrectWordChangingEventArgs e)
    {
        // Keep the TextChanged handler from checking while the dialog is open
        isColoring = true;
    
        TextRange range = GetRange(richTextBox1.Document, e.IncorrectWord);
    
        if (e.Type == TXSpell.ChangeType.Replace)
        {
            range.Text = e.CorrectedWord;
        }
        else if (e.Type == TXSpell.ChangeType.Delete)
        {
            range.Text = "";
        }
    
        isColoring = false;
    }
    

What the Code Does

  • SpellCheckDialog returns the dialog of the spell checker. Show(this) opens it with the words of the last Check, owned by the window.
  • Type says what the user did: Replace, Delete or Ignore, values of TXSpell.ChangeType. An ignored word needs no change in the text.
  • IncorrectWord is the word being changed. GetRange from Check Spelling as You Type finds its TextRange in the document, and setting the Text of the range replaces it.
  • CorrectedWord is the suggestion the user chose. Read it only when Type is Replace: for other types, it throws an exception.
  • Changing the document raises TextChanged, and its handler would call Check while the dialog is open, which throws an exception. The isColoring flag makes the handler skip these changes; the colors are updated again when the user types.

Run the Application

Press F5, type a sentence with a few mistakes, and click Spell Check Dialog.... The dialog opens with the first misspelled word in its sentence:

The Spelling dialog over a RichTextBox: the sentence with the misspelled word "sampke" in red, the language line "No language set", the suggestions and the buttons Ignore Once, Ignore All, Add To Dictionary, Change, Change All, Options and Cancel.

Select a suggestion and click Change: the word is replaced in the RichTextBox, and the dialog moves on to the next one. Ignore Once and Ignore All skip words, and Cancel ends the check. The dialog shows "No language set" because the text of a RichTextBox carries no language information.

Offer Suggestions in the Context Menu

The same words can also be corrected with a right-click. The GetMenuItems method returns ready-made menu items for an incorrect word, with suggestions and the commands of the dialog, to put into a ContextMenu:

The context menu of a misspelled word in a RichTextBox, with five suggestions, Ignore, Ignore All, Add To Dictionary, Spell Check Dialog and Options.

Context Menus builds this menu step by step.

Next Steps