# Events

> Handle TXSpellChecker events in WPF code-behind: IncorrectWordChanging, MisspelledWordChanging, dialog closing, OptionsDialogOpening and dictionary changes.

- **Product:** TX Spell .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Spell .NET for WPF > Spell Checking > Events
- **3095 words**
- **Web URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/events/
- **LLMs URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/events/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/events/llms-full.txt

---

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](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/llms-full.txt). 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](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/as-you-type/#use-the-spell-checker-in-code) shows: `txSpellChecker1.MisspelledWordChanging += txSpellChecker1_MisspelledWordChanging;`.

| Event | Occurs when | Arguments |
|---|---|---|
| [IncorrectWordChanging](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/incorrectwordchanging-event/llms-full.txt) | An [IncorrectWord](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/incorrectword/llms-full.txt) is about to be changed by the dialog or the context menu | [IncorrectWordChangingEventArgs](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/incorrectwordchangingeventargs/llms-full.txt) |
| [MisspelledWordChanging](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/misspelledwordchanging-event/llms-full.txt) | A `MisspelledWord` of TX Text Control is about to be changed by the dialog or the context menu | [MisspelledWordChangingEventArgs](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/misspelledwordchangingeventargs/llms-full.txt) |
| [MenuItemClicked](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/menuitemclicked-event/llms-full.txt) | A spell check menu item was clicked | [MenuItemClickedEventArgs](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/menuitemclickedeventargs/llms-full.txt) |
| [DialogButtonClicked](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/dialogbuttonclicked-event/llms-full.txt) | A button of the spell check dialog was clicked | [DialogButtonEventArgs](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/dialogbuttoneventargs/llms-full.txt) |
| [SpellCheckDialogOpening](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/spellcheckdialogopening-event/llms-full.txt) | The spell check dialog is opened with `SpellCheckDialog.Show` | [SpellCheckDialogOpeningEventArgs](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/spellcheckdialogopeningeventargs/llms-full.txt) |
| [SpellCheckDialogClosing](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/spellcheckdialogclosing-event/llms-full.txt) | The spell check dialog is closing | [SpellCheckDialogClosingEventArgs](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/spellcheckdialogclosingeventargs/llms-full.txt) |
| [OptionsDialogOpening](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/optionsdialogopening-event/llms-full.txt) | The options dialog is opened with `OptionsDialog` or the **Options...** button of the spell check dialog | [OptionsDialogOpeningEventArgs](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/optionsdialogopeningeventargs/llms-full.txt) |
| [DictionaryAdded](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/dictionaryadded-event/llms-full.txt) | A dictionary was added to `Dictionaries` | [DictionaryAddedEventArgs](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/dictionaryaddedeventargs/llms-full.txt) |
| [DictionaryRemoved](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/dictionaryremoved-event/llms-full.txt) | A dictionary was removed from `Dictionaries` | [DictionaryRemovedEventArgs](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/dictionaryremovedeventargs/llms-full.txt) |
| [UserDictionaryWordAdded](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/userdictionarywordadded-event/llms-full.txt) | A word was added to a user dictionary | [UserDictionaryWordAddedEventArgs](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/userdictionarywordaddedeventargs/llms-full.txt) |
| [UserDictionaryWordRemoved](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/userdictionarywordremoved-event/llms-full.txt) | A word was removed from a user dictionary | [UserDictionaryWordRemovedEventArgs](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/userdictionarywordremovedeventargs/llms-full.txt) |

The [TXSpell](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/txspell/llms-full.txt) 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](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/spellcheckdialog/llms-full.txt) opened with `Show`, and menus built from [GetMenuItems](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/getmenuitems-method/llms-full.txt). 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](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/on-demand/llms-full.txt) 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](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/txspell-changetype/enumeration/llms-full.txt):

| Value | The word is |
|---|---|
| `Replace` | Replaced by `CorrectedWord` |
| `Delete` | Deleted |
| `Ignore` | Ignored |

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:

```csharp
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:

- [MisspelledWord](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/misspelledwordchangingeventargs/misspelledword-property/llms-full.txt) is typed `object`; it holds a [TXTextControl.MisspelledWord](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/misspelledword/llms-full.txt). [IFormattedText](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/misspelledwordchangingeventargs/iformattedtext-property/llms-full.txt) is the text part that contains it.
- [Cancel](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/misspelledwordchangingeventargs/cancel-property/llms-full.txt) set to `true` leaves the word as it is.
- [CorrectedWord](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/misspelledwordchangingeventargs/correctedword-property/llms-full.txt) is read only after `Type` was tested for `Replace`.

`IncorrectWordChangingEventArgs` has the same `Type`, `CorrectedWord`, `WillBeDuplicate` and `Cancel`, plus [IncorrectWord](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/incorrectwordchangingeventargs/incorrectword-property/llms-full.txt) with its position, [SourceText](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/incorrectwordchangingeventargs/sourcetext-property/llms-full.txt), the text without the corrections, and [CorrectedText](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/incorrectwordchangingeventargs/correctedtext-property/llms-full.txt), the text with them. [Checking on Demand](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/on-demand/#check-a-textbox-in-the-dialog) 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:

- [MenuItemClickedEventArgs.MenuItem](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/menuitemclickedeventargs/menuitem-property/llms-full.txt) is the clicked [MenuItem](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/menuitem/llms-full.txt). Its `Type` says what the item does, as [What the Items Do](https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/context-menus/#what-the-items-do) lists.
- [DialogButtonEventArgs.DialogButton](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/dialogbuttoneventargs/dialogbutton-property/llms-full.txt) is the clicked [DialogButton](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/dialogbutton/llms-full.txt). Its `Type` says what the button does, as [What Each Button Does](https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/spell-check-dialog/#what-each-button-does) lists; `User` marks a button you added.

A `User` button is how a dialog button of your own does its work: [Customizing the Spell Check Dialog](https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/customizing-dialog/#add-a-button-of-your-own) adds one, and [Context Menus](https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/context-menus/llms-full.txt) 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](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/spellcheckdialogstatemanager/llms-full.txt) 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](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/text-parts/llms-full.txt) shows.
- In **`SpellCheckDialogClosing`**, setting [Cancel](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/spellcheckdialogclosingeventargs/cancel-property/llms-full.txt) to `true` keeps the dialog open, for example to continue with the next control, as [Checking Multiple Controls](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/multiple-controls/llms-full.txt) shows.

![The Spelling dialog over a TextControl, with Ignore Once, Ignore All, Add To Dictionary, Change, Change All, Options and Cancel.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-wpf/textcontrol-spelling-dialog.0a7b34fc.webp)

### Replace the Options Dialog

`OptionsDialogOpening` fires when the options dialog of TX Spell .NET is about to open, whether your code calls [OptionsDialog](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/optionsdialog-method/llms-full.txt) 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](https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/options-dialog/#replace-the-dialog-with-your-own) shows.

### Watch the Dictionaries

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

- **`DictionaryAdded`** and **`DictionaryRemoved`** carry the [Dictionary](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/dictionaryaddedeventargs/dictionary-property/llms-full.txt) that was added to or removed from [Dictionaries](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/dictionaries-property/llms-full.txt).
- **`UserDictionaryWordAdded`** carries the [UserDictionary](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/userdictionarywordaddedeventargs/userdictionary-property/llms-full.txt) and the [AddedWord](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/userdictionarywordaddedeventargs/addedword-property/llms-full.txt); **`UserDictionaryWordRemoved`** carries the user dictionary and the [RemovedWord](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/userdictionarywordremovedeventargs/removedword-property/llms-full.txt).

`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](https://docs.textcontrol.com/tx-spell-dotnet-wpf/dictionaries/user/llms-full.txt) covers loading and saving user dictionaries.

### Next Steps

- [Checking on Demand](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/on-demand/llms-full.txt): apply the corrections of the dialog to a `TextBox`.
- [Checking Multiple Controls](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/multiple-controls/llms-full.txt): keep the dialog open with `SpellCheckDialogClosing`.
- [Spelling Correction Modes and UI Provider](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/correction-modes/llms-full.txt): build a correction interface with data binding instead of events.
- [Customizing the Spell Check Dialog](https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/customizing-dialog/llms-full.txt): add buttons of your own and handle their clicks.

---

## Nearby

- **Previous:** [Spelling Correction Modes and UI Provider](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/correction-modes/llms-full.txt)
- **Next:** [Dictionaries and Languages](https://docs.textcontrol.com/tx-spell-dotnet-wpf/dictionaries/llms-full.txt)
