# Synonyms in the Context Menu

> Add a synonyms submenu to the WPF TextControl context menu with MenuItem objects in TextContextMenuOpening, or open ThesaurusDialog from a button of your own.

- **Product:** TX Spell .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Spell .NET for WPF > Synonyms and Thesaurus > Synonyms in the Context Menu
- **2439 words**
- **Web URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/synonyms/context-menu/
- **LLMs URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/synonyms/context-menu/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/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. The screenshots show TX Text Control Words, the Windows Forms sample application; the WPF dialog works the same way.

### 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-wpf/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-wpf/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-wpf/reference/txtextcontrol-wpf/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-wpf/synonyms/lists/llms-full.txt). Without a connected spell checker, 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-wpf/reference/txtextcontrol-wpf/thesaurusdialog/llms-full.txt) from a button or a menu item of your own. It takes the `TextControl` in its constructor:

```csharp
new TXTextControl.WPF.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-wpf/reference/txtextcontrol-wpf/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-wpf/reference/txtextcontrol-wpf/textcontrol/textcontextmenuopening-event/llms-full.txt) just before it opens a context menu. The [TextContextMenu](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontextmenueventargs/textcontextmenu-property/llms-full.txt) property of the arguments is the menu, a `ContextMenu`, to which you add `MenuItem` objects of `System.Windows.Controls`. This handler adds a **Synonyms** submenu for the selected word, and a **Thesaurus...** item that opens the dialog. `txSpellChecker1` is the resource of the window, which the constructor gets 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; attach the handler there too, with `textControl1.TextContextMenuOpening += textControl1_TextContextMenuOpening;`.

```csharp
private void textControl1_TextContextMenuOpening(object sender, TXTextControl.WPF.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);

    System.Windows.Controls.MenuItem synonymsItem = new System.Windows.Controls.MenuItem { Header = "Synonyms" };

    foreach (SynonymGroup group in groups)
    {
        foreach (Synonym synonym in group.Synonyms)
        {
            System.Windows.Controls.MenuItem item = new System.Windows.Controls.MenuItem { Header = synonym.Name };

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

    System.Windows.Controls.MenuItem thesaurusItem = new System.Windows.Controls.MenuItem { Header = "Thesaurus..." };
    thesaurusItem.Click += (s, args) => new TXTextControl.WPF.ThesaurusDialog(textControl1).ShowDialog();

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

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

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

The code works as follows:

- [ContextMenuLocation](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontextmenueventargs/contextmenulocation-property/llms-full.txt) says where the menu opens, as flags of [ContextMenuLocation](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/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-wpf/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-wpf/reference/txtextcontrol/selection/text-property/llms-full.txt), when it is a single word.
- [CreateSynonyms](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/createsynonyms-method/llms-full.txt) returns one [SynonymGroup](https://docs.textcontrol.com/tx-spell-dotnet-wpf/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-wpf/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-wpf/reference/txtextcontrol-proofing/synonym/llms-full.txt) of a group becomes a menu item. Its [Name](https://docs.textcontrol.com/tx-spell-dotnet-wpf/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.

`SynonymGroup` and `Synonym` are types of the namespace `TXTextControl.Proofing`, which the code-behind needs as a `using` directive. `MenuItem` is written with its namespace, `System.Windows.Controls.MenuItem`, because the namespace `TXTextControl.WPF.Proofing` of the spell checker has a class of that name as well.

[Looking Up Synonyms in Code](https://docs.textcontrol.com/tx-spell-dotnet-wpf/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-wpf/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-wpf/synonyms/in-code/llms-full.txt): create synonyms for any word.
- [Context Menus](https://docs.textcontrol.com/tx-spell-dotnet-wpf/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-wpf/synonyms/lists/llms-full.txt)
- **Next:** [Looking Up Synonyms in Code](https://docs.textcontrol.com/tx-spell-dotnet-wpf/synonyms/in-code/llms-full.txt)
