# Synonyms in the Context Menu

> Offer synonyms in a Windows Forms TextControl: the Thesaurus button and Thesaurus Dialog, and a synonyms submenu added in TextContextMenuOpening.

- **Product:** TX Spell .NET for Windows Forms
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Spell .NET for Windows Forms > Synonyms and Thesaurus > Synonyms in the Context Menu
- **2172 words**
- **Web URL:** https://docs.textcontrol.com/tx-spell-dotnet-winforms/synonyms/context-menu/
- **LLMs URL:** https://docs.textcontrol.com/tx-spell-dotnet-winforms/synonyms/context-menu/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-spell-dotnet-winforms/synonyms/context-menu/llms-full.txt

---

With TX Spell .NET connected, TX Text Control offers synonyms through its thesaurus: the user selects a word, clicks **Thesaurus** on the ribbon and picks a replacement in the Thesaurus Dialog. The built-in context menu of a word has no synonyms, but it can have them: TX Text Control lets your code extend the menu before it opens, and the spell checker creates the synonyms to put into it.

### What the User Sees

The **Thesaurus** button is in the **Spelling & Hyphenation** group of the ribbon's **Proofing** tab. With a word selected, it opens the Thesaurus Dialog, and **Replace** puts the chosen synonym in place of the word:

![TX Text Control Words: the word "powerful" is selected and the Thesaurus button of the Proofing tab is clicked, the Thesaurus Dialog lists synonyms such as almighty, all-powerful and compelling, and after Replace the sentence reads "TX Text Control is a compelling word processor."](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-winforms/thesaurus-dialog.7ed740d6.webp)

The dialog shows:

- **Find Synonyms for**, the word, which the user can change to look up another one.
- **Language**, the language of the synonyms, with one entry for each language that has a thesaurus loaded.
- **Synonyms**, a tree with one group per meaning. A double-click on a synonym, or the space bar, looks up the synonyms of that synonym, and the back arrow returns to the previous word.
- **Replace** and **Cancel**. **Replace**, or Enter, puts the selected synonym in place of the selected text.

![The Thesaurus Dialog with Find Synonyms for: powerful, Language: English (United States), and the synonym compelling selected in the tree.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-winforms/thesaurus-dialog-open.d7429a4a.webp)

When no word is selected, the dialog takes the word at the input position.

### How It Works

The dialog and the ribbon button belong to TX Text Control; the synonyms come from TX Spell .NET. The spell checker connected to the `TextControl` through its [SpellChecker](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/spellchecker-property/llms-full.txt) property creates them from its synonym lists, the thesaurus files described in [Synonym Lists](https://docs.textcontrol.com/tx-spell-dotnet-winforms/synonyms/lists/llms-full.txt). Without a connected spell checker, the ribbon buttons of the **Proofing** tab, except the language button, are disabled, and the dialog throws an exception.

### Open the Thesaurus Dialog in Code

For an application without a ribbon, open the [ThesaurusDialog](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/thesaurusdialog/llms-full.txt) from a button or a menu item of your own. It takes the `TextControl` in its constructor:

```csharp
new TXTextControl.ThesaurusDialog(textControl1).ShowDialog();
```

The dialog works on the selected text of the `TextControl`, or on the word at the input position, which it selects. The [RibbonProofingTab](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-windows-forms-ribbon/ribbonproofingtab/llms-full.txt) opens the same dialog from its **Thesaurus** button.

### Add Synonyms to the Context Menu

TX Text Control raises [TextContextMenuOpening](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/textcontextmenuopening-event/llms-full.txt) just before it opens a context menu. The [TextContextMenu](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontextmenueventargs/textcontextmenu-property/llms-full.txt) property of the arguments is the menu, a `ContextMenuStrip`, to which you add `ToolStripMenuItem` objects. This handler adds a **Synonyms** submenu for the selected word, and a **Thesaurus...** item that opens the dialog. Attach the handler to the `TextContextMenuOpening` event of *textControl1*, in the events list of the **Properties** window or in the constructor.

```csharp
private void textControl1_TextContextMenuOpening(object sender, TXTextControl.TextContextMenuEventArgs e)
{
    // Leave the spell check menu of a misspelled word as it is
    if ((e.ContextMenuLocation & TXTextControl.ContextMenuLocation.MisspelledWord) != 0) return;

    // Offer synonyms for a single selected word
    string word = textControl1.Selection.Text.Trim();
    if (word.Length == 0 || word.Contains(" ")) return;

    // Use the language of the text, or all synonym lists when it has none
    SynonymGroup[] groups = textControl1.Selection.Culture != null
        ? txSpellChecker1.CreateSynonyms(word, textControl1.Selection.Culture)
        : txSpellChecker1.CreateSynonyms(word);

    ToolStripMenuItem synonymsItem = new ToolStripMenuItem("Synonyms");

    foreach (SynonymGroup group in groups)
    {
        foreach (Synonym synonym in group.Synonyms)
        {
            ToolStripMenuItem item = new ToolStripMenuItem(synonym.Name);

            // Replace the selected word with the synonym
            item.Click += (s, args) => textControl1.Selection.Text = synonym.Name;
            synonymsItem.DropDownItems.Add(item);
        }
    }

    ToolStripMenuItem thesaurusItem = new ToolStripMenuItem("Thesaurus...");
    thesaurusItem.Click += (s, args) => new TXTextControl.ThesaurusDialog(textControl1).ShowDialog();

    e.TextContextMenu.Items.Add(new ToolStripSeparator());

    if (synonymsItem.DropDownItems.Count > 0)
    {
        e.TextContextMenu.Items.Add(synonymsItem);
    }

    e.TextContextMenu.Items.Add(thesaurusItem);
}
```

The code works as follows:

- `SynonymGroup` and `Synonym` are types of the namespace `TXTextControl.Proofing`; add `using TXTextControl.Proofing;` to the file of the form.
- [ContextMenuLocation](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontextmenueventargs/contextmenulocation-property/llms-full.txt) says where the menu opens, as flags of [ContextMenuLocation](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/contextmenulocation/enumeration/llms-full.txt). A menu over a misspelled word is the spell check menu, which the handler leaves alone; [Context Menus](https://docs.textcontrol.com/tx-spell-dotnet-winforms/user-interface/context-menus/llms-full.txt) covers that menu.
- The synonyms are for the selected text, [Selection.Text](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/text-property/llms-full.txt), when it is a single word.
- [CreateSynonyms](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/createsynonyms-method/llms-full.txt) returns one [SynonymGroup](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/synonymgroup/llms-full.txt) per meaning. With the language of the text, [Selection.Culture](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/culture-property/llms-full.txt), it uses the synonym lists of that language; text without a language uses all lists.
- Each [Synonym](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/synonym/llms-full.txt) of a group becomes a menu item. Its [Name](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/synonym/name-property/llms-full.txt) is the synonym itself; a click writes it into the selection.
- The labels **Synonyms** and **Thesaurus...** are the code's own; choose them to fit your application.

[Looking Up Synonyms in Code](https://docs.textcontrol.com/tx-spell-dotnet-winforms/synonyms/in-code/llms-full.txt) describes `CreateSynonyms` and the synonym groups in detail.

### Next Steps

- [Synonym Lists](https://docs.textcontrol.com/tx-spell-dotnet-winforms/synonyms/lists/llms-full.txt): the thesaurus files and how a list is chosen.
- [Looking Up Synonyms in Code](https://docs.textcontrol.com/tx-spell-dotnet-winforms/synonyms/in-code/llms-full.txt): create synonyms for any word.
- [Context Menus](https://docs.textcontrol.com/tx-spell-dotnet-winforms/user-interface/context-menus/llms-full.txt): the spell check menu and menus of your own.

---

## Nearby

- **Previous:** [Synonym Lists](https://docs.textcontrol.com/tx-spell-dotnet-winforms/synonyms/lists/llms-full.txt)
- **Next:** [Looking Up Synonyms in Code](https://docs.textcontrol.com/tx-spell-dotnet-winforms/synonyms/in-code/llms-full.txt)
