# Context Menus

> Use the built-in spell check menu of a Windows Forms TextControl or build a ContextMenuStrip with GetMenuItems and MenuItemClicked, also for a RichTextBox.

- **Product:** TX Spell .NET for Windows Forms
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Spell .NET for Windows Forms > User Interface > Context Menus
- **3015 words**
- **Web URL:** https://docs.textcontrol.com/tx-spell-dotnet-winforms/user-interface/context-menus/
- **LLMs URL:** https://docs.textcontrol.com/tx-spell-dotnet-winforms/user-interface/context-menus/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-spell-dotnet-winforms/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 build one from ready-made menu items. The sections below cover the built-in menu, how to replace it, how to build your own and how to react to its items.

### The Built-In Menu

When a [TXSpellChecker](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/llms-full.txt) is connected to a `TextControl` through its [SpellChecker](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/spellchecker-property/llms-full.txt) property and [IsSpellCheckingEnabled](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/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 on one form, each with red underlines, and the context menu of a misspelled word with suggestions, Ignore, Ignore All, Add to Dictionary, Spell Check Dialog and Options.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-winforms/four-textcontrols-context.0b87ecfe.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-winforms/dictionaries/user/llms-full.txt) shows. [Correct Words from the Context Menu](https://docs.textcontrol.com/tx-spell-dotnet-winforms/getting-started/tx-text-control/context-menu/llms-full.txt) connects the spell checker step by step.

### Choose Which Menu Appears

The [SpellCheckContextMenuStrip](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/spellcheckcontextmenustrip-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 name of a `ContextMenuStrip` | Your own menu |

Set it in the **Properties** window of the designer or in code. It works only while a spell checker is connected through `SpellChecker`.

### Build Your Own Menu

[GetMenuItems](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/getmenuitems-method/llms-full.txt) creates the spelling items 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 a menu, together with items of your own. 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-winforms/textcontrol-context-menu.5b60b5a7.webp)

This form builds a similar menu in code. It runs in a new Windows Forms project with the TX Text Control and TX Spell .NET packages; add a `Main` method or create the form from *Program.cs*:

```csharp
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using TXTextControl;
using TXTextControl.Proofing;

public class MenuForm : Form
{
    private readonly TextControl textControl1 = new TextControl();
    private readonly TXSpellChecker txSpellChecker1 = new TXSpellChecker();
    private readonly ContextMenuStrip spellMenu = new ContextMenuStrip();

    public MenuForm()
    {
        textControl1.Dock = DockStyle.Fill;
        textControl1.SpellChecker = txSpellChecker1;
        textControl1.IsSpellCheckingEnabled = true;

        // Use this menu for misspelled words
        spellMenu.Name = "spellMenu";
        spellMenu.Opening += spellMenu_Opening;
        textControl1.SpellCheckContextMenuStrip = "spellMenu";

        Controls.Add(textControl1);
    }

    private void spellMenu_Opening(object sender, CancelEventArgs e)
    {
        spellMenu.Items.Clear();

        // The mouse position in pixels, relative to the control
        Point location = textControl1.PointToClient(MousePosition);

        MisspelledWord word = textControl1.MisspelledWords.GetItem(location);
        if (word == null)
        {
            e.Cancel = true;
            return;
        }

        // Up to five suggestions, Ignore, Ignore All and Add To Dictionary
        spellMenu.Items.AddRange(txSpellChecker1.GetMenuItems(word, textControl1, 5));

        // Items of your own
        spellMenu.Items.Add(new ToolStripSeparator());
        spellMenu.Items.Add("Spell Check Dialog...", null, (s, args) => txSpellChecker1.SpellCheckDialog.Show(textControl1));
        spellMenu.Items.Add("Options...", null, (s, args) => txSpellChecker1.OptionsDialog());
    }
}
```

The code works as follows:

- `SpellCheckContextMenuStrip` names the menu by its `Name`, so the menu needs one.
- The `Opening` event of the menu fills it anew for each right-click. [GetItem](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/misspelledwordcollection/getitem-method/llms-full.txt) of the [MisspelledWords](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/misspelledwords-property/llms-full.txt) collection returns the misspelled word at a location, or `null`. The location is given in pixels, with its origin at the upper-left corner of the control.
- `GetMenuItems` takes the word, the `TextControl` and the maximum number of suggestions. It returns [MenuItem](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/menuitem/llms-full.txt) objects, which are `ToolStripMenuItem` objects, so `AddRange` adds them to any `ContextMenuStrip`.
- `GetMenuItems` doesn't create **Spell Check Dialog...** and **Options...**. The last two items call [SpellCheckDialog.Show](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/spellcheckdialog/show-method/llms-full.txt) and [OptionsDialog](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/optionsdialog-method/llms-full.txt), the dialogs of TX Spell .NET.

The items can also go into a menu your application already has, next to its own commands.

### What the Items Do

The [Type](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/menuitem/type-property/llms-full.txt) of each `MenuItem` is a value of [TXSpell.MenuItemType](https://docs.textcontrol.com/tx-spell-dotnet-winforms/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-winforms/reference/txtextcontrol-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)
    {
        statusLabel.Text = "Word added to the dictionary.";
    }
};
```

[MenuItem](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/menuitemclickedeventargs/menuitem-property/llms-full.txt) of the [MenuItemClickedEventArgs](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/menuitemclickedeventargs/llms-full.txt) is the clicked item. `statusLabel` stands for a label of your form. [Events](https://docs.textcontrol.com/tx-spell-dotnet-winforms/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-winforms/reference/txtextcontrol-proofing/incorrectword/llms-full.txt), found by [Check](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/check-method/llms-full.txt), and the text that contains it:

```csharp
TXTextControl.Proofing.MenuItem[] items = txSpellChecker1.GetMenuItems(currentIncorrectWord, richTextBox1.Text, 5);
```

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

![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-winforms/richtextbox-context-menu.f03e53b3.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-winforms/reference/txtextcontrol-proofing/txspellchecker/incorrectwordchanging-event/llms-full.txt) event fires, and your handler replaces the word in the control, as [Open the Spell Check Dialog](https://docs.textcontrol.com/tx-spell-dotnet-winforms/getting-started/richtextbox/dialog/llms-full.txt) shows for the dialog. The *RichTextBox\\Context menu strip* sample of the setup shows the whole menu; [Spell Checking Examples](https://docs.textcontrol.com/tx-spell-dotnet-winforms/examples/spell-checking/llms-full.txt) lists it.

### Next Steps

- [Synonyms in the Context Menu](https://docs.textcontrol.com/tx-spell-dotnet-winforms/synonyms/context-menu/llms-full.txt): offer synonyms of a word from a menu.
- [Spell Check Dialog](https://docs.textcontrol.com/tx-spell-dotnet-winforms/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-winforms/user-interface/options-dialog/llms-full.txt): the dialog behind **Options...**.
- [Localization and Resource Kit](https://docs.textcontrol.com/tx-spell-dotnet-winforms/user-interface/localization/llms-full.txt): the texts of TX Spell .NET in other languages.
- [Events](https://docs.textcontrol.com/tx-spell-dotnet-winforms/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-winforms/user-interface/options-dialog/llms-full.txt)
- **Next:** [Suggestion Controls](https://docs.textcontrol.com/tx-spell-dotnet-winforms/user-interface/suggestions/llms-full.txt)
