# Context Menus

> Replace the WPF spell check menu with a ContextMenu resource and SpellCheckContextMenu, add items from GetMenuItems and handle MenuItemClicked in code-behind.

- **Product:** TX Spell .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Spell .NET for WPF > User Interface > Context Menus
- **2943 words**
- **Web URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/context-menus/
- **LLMs URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/context-menus/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/context-menus/llms-full.txt

---

Most users correct a misspelled word without leaving the text: they right-click the underlined word and choose the right spelling from a menu. With TX Text Control, TX Spell .NET provides this menu without any code. When you need other items in it, or a menu for another control, you declare your own `ContextMenu` and fill it with ready-made menu items. The sections below cover the built-in menu, how to replace it and how to react to its items.

### The Built-In Menu

When a [TXSpellChecker](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/llms-full.txt) is connected to a `TextControl` through its [SpellChecker](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/spellchecker-property/llms-full.txt) property and [IsSpellCheckingEnabled](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/isspellcheckingenabled-property/llms-full.txt) is `true`, misspelled words get a red zigzag line, and a right-click on one of them opens the spell check menu of TX Text Control instead of the usual context menu:

![Four TextControls in one window, with red underlines under misspelled words, and the context menu of the misspelled word "haeder" with suggestions, Ignore, Ignore All, Add to Dictionary, Spell Check Dialog and Options.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-wpf/four-textcontrols-context.bfb63dae.webp)

From the menu, the user:

- replaces the word with one of the suggestions at the top;
- ignores the word here or everywhere, with **Ignore** and **Ignore All**;
- adds it to a user dictionary with **Add to Dictionary**;
- opens the spell check dialog of TX Text Control with **Spell Check Dialog...**;
- opens the options dialog with **Options...**.

**Add to Dictionary** is enabled only when a user dictionary has been added to the dictionaries of the spell checker, as [User Dictionaries](https://docs.textcontrol.com/tx-spell-dotnet-wpf/dictionaries/user/llms-full.txt) shows. [Correct Words from the Context Menu](https://docs.textcontrol.com/tx-spell-dotnet-wpf/getting-started/tx-text-control/context-menu/llms-full.txt) connects the spell checker step by step.

### Choose Which Menu Appears

The [SpellCheckContextMenu](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/spellcheckcontextmenu-property/llms-full.txt) property of the `TextControl` decides what a right-click on a misspelled word opens. It is a string:

| Value | Menu |
|---|---|
| `(Default)` | The built-in menu shown above |
| `(none)` | No menu |
| The key of a `ContextMenu` resource | Your own menu |

A menu of your own must be a resource of the `TextControl`, or of an element that contains it, such as the window. This XAML declares a menu in the resources of the `TextControl` and selects it by its key:

```xml
<Window x:Class="SpellingApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:tx="clr-namespace:TXTextControl.WPF;assembly=TXTextControl.WPF"
        xmlns:spell="clr-namespace:TXTextControl.WPF.Proofing;assembly=TXSpell.WPF"
        Title="Spelling" Height="450" Width="800">
    <Window.Resources>
        <spell:TXSpellChecker x:Key="txSpellChecker1" />
    </Window.Resources>
    <tx:TextControl Name="textControl1"
                    IsSpellCheckingEnabled="True"
                    SpellChecker="txSpellChecker1"
                    SpellCheckContextMenu="UserSpellCheckMenu">
        <tx:TextControl.Resources>
            <ContextMenu x:Key="UserSpellCheckMenu">
                <MenuItem Header="Spell Check Dialog..." Click="SpellCheckDialog_Click" />
                <MenuItem Header="Options..." Click="Options_Click" />
            </ContextMenu>
        </tx:TextControl.Resources>
    </tx:TextControl>
</Window>
```

The two items open the dialogs of TX Spell .NET. Their handlers go into the code-behind, where `txSpellChecker1` is the spell checker taken from the resources with `FindResource`, as [As-You-Type Checking](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/as-you-type/llms-full.txt) shows:

```csharp
private void SpellCheckDialog_Click(object sender, RoutedEventArgs e)
{
    txSpellChecker1.SpellCheckDialog.Show(textControl1);
}

private void Options_Click(object sender, RoutedEventArgs e)
{
    txSpellChecker1.OptionsDialog();
}
```

[SpellCheckDialog.Show](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/spellcheckdialog/show-method/llms-full.txt) opens the [Spell Check Dialog](https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/spell-check-dialog/llms-full.txt) and [OptionsDialog](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/optionsdialog-method/llms-full.txt) the [Options Dialog](https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/options-dialog/llms-full.txt).

### Add the Spelling Items

A menu of your own also needs the suggestions for the word and the commands that correct it. [GetMenuItems](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/getmenuitems-method/llms-full.txt) creates them for a misspelled word: its suggestions, **Ignore**, **Ignore All** and **Add To Dictionary**. Each item carries out its action when it is clicked, so you only add the items to the menu. The *TextControl\\Context menu strip* sample of the setup builds its menu this way:

![The spell check menu of the Context menu strip sample over the misspelled word "sampke": the suggestions sample, spake, smoke, samples and sampler, then Ignore, Ignore All and a disabled Add To Dictionary, then Spell Check Dialog and Options.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-wpf/textcontrol-context-menu.e3692bcd.webp)

For a misspelled word of the `TextControl`, a `MisspelledWord`, the call takes the word, the `TextControl` and the largest number of suggestions:

```csharp
TXTextControl.WPF.Proofing.MenuItem[] items = txSpellChecker1.GetMenuItems(misspelledWord, textControl1, 5);
```

It returns [MenuItem](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/menuitem/llms-full.txt) objects, which are WPF `MenuItem` objects, so you add them to the `Items` of the `ContextMenu` together with items of your own. `GetMenuItems` doesn't create **Spell Check Dialog...** and **Options...**; those are items like the two in the XAML above. The sample of the setup shows the complete menu; [Spell Checking Examples](https://docs.textcontrol.com/tx-spell-dotnet-wpf/examples/spell-checking/llms-full.txt) lists it.

### What the Items Do

The [Type](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/menuitem/type-property/llms-full.txt) of each `MenuItem` is a value of [TXSpell.MenuItemType](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/txspell-menuitemtype/enumeration/llms-full.txt), and it decides what a click does:

| Type | What a click does |
|---|---|
| `Suggestion` | Replaces the misspelled word with the text of the item |
| `IgnoreOnce` | Ignores this occurrence of the word |
| `IgnoreAll` | Ignores every occurrence of the word |
| `AddToDictionary` | Adds the word to all editable user dictionaries |
| `Delete` | Removes the word from the text |
| `NoSuggestions` | Nothing; the item says that no suggestions were found |

After each click, the [MenuItemClicked](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/menuitemclicked-event/llms-full.txt) event of the `TXSpellChecker` reports the item, for example to log words that users add:

```csharp
txSpellChecker1.MenuItemClicked += (sender, e) =>
{
    if (e.MenuItem.Type == TXSpell.MenuItemType.AddToDictionary)
    {
        statusText.Text = "Word added to the dictionary.";
    }
};
```

[MenuItem](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/menuitemclickedeventargs/menuitem-property/llms-full.txt) of the [MenuItemClickedEventArgs](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/menuitemclickedeventargs/llms-full.txt) is the clicked item. `statusText` stands for a `TextBlock` of your window, and `TXSpell` needs `using TXTextControl.Proofing;`. [Events](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/events/llms-full.txt) lists the other events a correction raises.

### A Menu for a RichTextBox

A `RichTextBox` or a `TextBox` has no built-in spell check menu, but `GetMenuItems` works for it, too. Its second overload takes an [IncorrectWord](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/incorrectword/llms-full.txt), found by [Check](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/check-method/llms-full.txt), and the text that contains it. For a `RichTextBox`, the text is the content of its document:

```csharp
TXTextControl.WPF.Proofing.MenuItem[] items = txSpellChecker1.GetMenuItems(
    currentIncorrectWord,
    new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text,
    5);
```

Without a connected `TextControl`, the spell checker isn't initialized for you: call its [Create](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/create-method/llms-full.txt) method once before the first check, for example in the constructor of the window.

![The spell check menu over the misspelled word "sampke" in a RichTextBox, with the suggestions sample, spake, smoke, samples and sampler, then Ignore, Ignore All, Add To Dictionary, Spell Check Dialog and Options.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-wpf/richtextbox-context-menu.e10b1dc8.webp)

The spell checker can't change the text of a `RichTextBox` itself. When the user picks a suggestion, the [IncorrectWordChanging](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/incorrectwordchanging-event/llms-full.txt) event fires, and your handler replaces the word in the control, as [Checking on Demand](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/on-demand/#check-a-textbox-in-the-dialog) shows for the dialog. The *RichTextBox\\Context menu strip* sample of the setup shows the whole menu.

### Next Steps

- [Synonyms in the Context Menu](https://docs.textcontrol.com/tx-spell-dotnet-wpf/synonyms/context-menu/llms-full.txt): offer synonyms of a word from a menu.
- [Spell Check Dialog](https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/spell-check-dialog/llms-full.txt): the dialog of TX Spell .NET that the menu can open.
- [Options Dialog](https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/options-dialog/llms-full.txt): the dialog behind **Options...**.
- [Localization and Resource Kit](https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/localization/llms-full.txt): the texts of TX Spell .NET in other languages.
- [Events](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/events/llms-full.txt): react to the corrections made in the menu.

---

## Nearby

- **Previous:** [Options Dialog](https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/options-dialog/llms-full.txt)
- **Next:** [Suggestion Controls](https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/suggestions/llms-full.txt)
