TX Spell .NET for WPF

Events

Topic

Every click in the spell check dialog and in the spell check context menu of TX Spell .NET raises an event before anything changes: a word is about to be replaced, ignored or deleted, a button or menu item was clicked, a dialog is opening or closing. Handlers of these events apply corrections to controls other than TX Text Control, log what users change, keep a dialog open for the next control, or replace a built-in dialog with your own. The dictionaries report their changes through events, too.

The Events at a Glance

All events belong to the TXSpellChecker. The spell checker is a resource of the window, so attach the handlers in code with +=, after the code-behind got it with FindResource, as As-You-Type Checking shows: txSpellChecker1.MisspelledWordChanging += txSpellChecker1_MisspelledWordChanging;.

EventOccurs whenArguments
IncorrectWordChangingAn IncorrectWord is about to be changed by the dialog or the context menuIncorrectWordChangingEventArgs
MisspelledWordChangingA MisspelledWord of TX Text Control is about to be changed by the dialog or the context menuMisspelledWordChangingEventArgs
MenuItemClickedA spell check menu item was clickedMenuItemClickedEventArgs
DialogButtonClickedA button of the spell check dialog was clickedDialogButtonEventArgs
SpellCheckDialogOpeningThe spell check dialog is opened with SpellCheckDialog.ShowSpellCheckDialogOpeningEventArgs
SpellCheckDialogClosingThe spell check dialog is closingSpellCheckDialogClosingEventArgs
OptionsDialogOpeningThe options dialog is opened with OptionsDialog or the Options... button of the spell check dialogOptionsDialogOpeningEventArgs
DictionaryAddedA dictionary was added to DictionariesDictionaryAddedEventArgs
DictionaryRemovedA dictionary was removed from DictionariesDictionaryRemovedEventArgs
UserDictionaryWordAddedA word was added to a user dictionaryUserDictionaryWordAddedEventArgs
UserDictionaryWordRemovedA word was removed from a user dictionaryUserDictionaryWordRemovedEventArgs

The TXSpell class, for checking in code, has no events.

Which Dialog and Menu Raise Them

The events of the dialog and the context menu come from the user interface of TX Spell .NET: the SpellCheckDialog opened with Show, and menus built from GetMenuItems. The preconfigured dialog and context menu of TX Text Control are opened by TX Text Control and change words themselves; MisspelledWordChanging is not raised for their changes. To handle every correction in a TextControl, open the dialog of TX Spell .NET, as Checking on Demand shows.

Which of the two "changing" events fires depends on the kind of word:

  • MisspelledWordChanging fires for the words of a TextControl. The spell checker applies the change to the document itself; the event lets you watch or stop it.
  • IncorrectWordChanging fires for the words found by Check in any other text. The spell checker cannot reach your control, so your handler must apply the change.

Apply or Cancel a Change

Both "changing" events say what is about to happen through Type, a value of TXSpell.ChangeType:

ValueThe word is
ReplaceReplaced by CorrectedWord
DeleteDeleted
IgnoreIgnored

The arguments carry the word and, for Replace, the replacement. CorrectedWord and WillBeDuplicate throw an exception when Type is not Replace, so test Type first. Setting Cancel to true stops the change.

This handler keeps users from deleting words in a TextControl with the dialog of TX Spell .NET, and logs every replacement:

private void txSpellChecker1_MisspelledWordChanging(object sender, TXTextControl.Proofing.MisspelledWordChangingEventArgs e)
{
    if (e.Type == TXTextControl.Proofing.TXSpell.ChangeType.Delete)
    {
        // Keep the word in the document
        e.Cancel = true;
        return;
    }

    if (e.Type == TXTextControl.Proofing.TXSpell.ChangeType.Replace)
    {
        TXTextControl.MisspelledWord word = (TXTextControl.MisspelledWord)e.MisspelledWord;
        System.Diagnostics.Debug.WriteLine($"{word.Text} -> {e.CorrectedWord}");
    }
}

The code works as follows:

IncorrectWordChangingEventArgs has the same Type, CorrectedWord, WillBeDuplicate and Cancel, plus IncorrectWord with its position, SourceText, the text without the corrections, and CorrectedText, the text with them. Checking on Demand shows the handler that applies each change to a TextBox.

React to Menu Items and Dialog Buttons

MenuItemClicked and DialogButtonClicked tell which command the user chose, independent of the word:

A User button is how a dialog button of your own does its work: Customizing the Spell Check Dialog adds one, and Context Menus builds menus with GetMenuItems.

Follow the Dialog From Opening to Closing

The spell check dialog of TX Spell .NET raises SpellCheckDialogOpening when it opens and SpellCheckDialogClosing when it closes. Both arguments, and those of DialogButtonClicked, have a StateManager: a SpellCheckDialogStateManager that tells which word the dialog handles next and sets another one with SetNext.

  • In SpellCheckDialogOpening, the handler can make the dialog begin at another word or text part, as Checking Text Parts shows.
  • In SpellCheckDialogClosing, setting Cancel to true keeps the dialog open, for example to continue with the next control, as Checking Multiple Controls shows.

The Spelling dialog over a TextControl, with Ignore Once, Ignore All, Add To Dictionary, Change, Change All, Options and Cancel.

Replace the Options Dialog

OptionsDialogOpening fires when the options dialog of TX Spell .NET is about to open, whether your code calls OptionsDialog or the user clicks Options... in the spell check dialog. Setting Cancel to true stops it, so that your code can show its own settings window instead, as Replace the Dialog With Your Own shows.

Watch the Dictionaries

The dictionary events report changes to the dictionaries, whoever makes them:

UserDictionaryWordAdded also fires when the user clicks Add To Dictionary, which makes it the place to store the new word, for example in a database shared by all users. User Dictionaries covers loading and saving user dictionaries.

Next Steps