# Context Menus

> Extend, replace or suppress the built-in context menus of the Windows Forms TextControl with TextContextMenuOpening, ContextMenuLocation and ContextMenuStrip.

- **Product:** TX Text Control .NET for Windows Forms
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for Windows Forms > Document Editor > Ribbon and UI > Context Menus
- **2460 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/context-menus/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/context-menus/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/context-menus/llms-full.txt

---

A right-click in the `TextControl` opens a context menu whose items depend on what was clicked: a text selection, a table, a header, a field or an image offers different commands. The menus are built in and need no code. What follows shows how to add your own items, how to remove or replace a menu and how the menu of misspelled words and of drawings is chosen.

### How the Built-In Menus Work

The `TextControl` has context menus for different parts of a document. The following screenshot of the TX Words sample shows the menu of a table of contents: the clipboard commands, the formatting dialog boxes and the commands of the table of contents:

![A document in TX Words with a table of contents and its context menu, showing Cut, Copy, Paste, Character, Paragraph, Bullets and Numbering, Styles, Change Table of Contents, Update Table of Contents, Delete Table of Contents and Insert.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/context-menu-toc.85c56f26.webp)

Immediately before a built-in menu opens, the `TextControl` raises the [TextContextMenuOpening](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/textcontextmenuopening-event/llms-full.txt) event. Its [TextContextMenuEventArgs](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontextmenueventargs/llms-full.txt) tell you where the menu opens and give you the menu itself:

| Member | Purpose |
|---|---|
| [ContextMenuLocation](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontextmenueventargs/contextmenulocation-property/llms-full.txt) | The part of the document the menu is opened for, as a combination of [ContextMenuLocation](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/contextmenulocation/enumeration/llms-full.txt) values |
| [TextContextMenu](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontextmenueventargs/textcontextmenu-property/llms-full.txt) | The menu that will be shown, a `System.Windows.Forms.ContextMenuStrip`. You can change its items or assign another menu |
| [Location](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontextmenueventargs/location-property/llms-full.txt) | The position of the menu in pixels, relative to the top-left corner of the screen |
| `Cancel` | Set it to `true` and no menu is shown |

Because the menu is a standard `ContextMenuStrip`, you work with its `Items` collection, `ToolStripMenuItem` and `ToolStripSeparator` as in any Windows Forms menu.

### Where the Menu Opens

`ContextMenuLocation` can hold several values at once, for example a text selection inside a table. Test a single value with the bitwise `&` operator. The enumeration has these values:

| Value | The menu opens for |
|---|---|
| `TextSelection` | A text selection |
| `TextInputPosition` | The text input position |
| `Header`, `NoHeader` | The top page margin with or without a header |
| `Footer`, `NoFooter` | The bottom page margin with or without a footer |
| `PageMargin` | The top or the bottom page margin |
| `PageNumberField` | A page number field |
| `SelectedFrame` | A selected frame: a text frame, an image, a chart or a barcode |
| `Table` | A selection or the input position in a table |
| `TextField` | A selection or the input position in a text field; the reference lists this value as `Field`, which does not compile |
| `MisspelledWord` | A misspelled word |
| `TableOfContents` | A table of contents |
| `TrackedChange` | A tracked change |
| `Comment` | Commented text |
| `FormField` | A form field |
| `Footnote` | A footnote reference mark |

### Add Items to a Menu

The following code adds a separator and a **Table Properties...** item to every menu that opens in a table. The item opens the table formatting dialog box of TX Text Control. Add it to *Form1.cs* of an application with a `TextControl` named `textControl1`, as in [Create Your First Windows Forms Application](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-editor/first-application/llms-full.txt):

```csharp
using System.Windows.Forms;
using TXTextControl;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        textControl1.TextContextMenuOpening += TextControl1_TextContextMenuOpening;
    }

    private void TextControl1_TextContextMenuOpening(object sender, TextContextMenuEventArgs e)
    {
        // Extend only the menus that open in a table
        if ((e.ContextMenuLocation & ContextMenuLocation.Table) != 0)
        {
            ToolStripMenuItem propertiesItem = new ToolStripMenuItem("Table Properties...");
            propertiesItem.Click += (s, args) => textControl1.TableFormatDialog();

            e.TextContextMenu.Items.Add(new ToolStripSeparator());
            e.TextContextMenu.Items.Add(propertiesItem);
        }
    }
}
```

Keep the `namespace` that Visual Studio created around the class.

The code works as follows:

- The constructor connects the handler to `TextContextMenuOpening` after `InitializeComponent` has created the control.
- `ContextMenuLocation.Table` is tested with `&`, because the value can be combined with others such as `TextSelection`.
- `e.TextContextMenu.Items.Add` appends the new items to the menu before it is shown.
- [TableFormatDialog](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/tableformatdialog-method/llms-full.txt) opens the built-in table dialog box; [Built-in Dialog Boxes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/dialog-boxes/llms-full.txt) lists the others.

The TX Words sample *Context Menu* of the setup works the same way: when a frame is selected (`ContextMenuLocation.SelectedFrame`), it adds a separator and a **Frame Name...** item that opens a dialog box of its own for the name of the frame:

![The Enter Frame Name dialog box of the TX Words Context Menu sample in front of a selected drawing, with the name Smiley in its Frame name box.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/frame-name-dialog.3166cb8b.webp)

The sample is installed in *%USERPROFILE%\\Documents\\TX Text Control 34.0.NET for Windows Forms\\CSharp\\TX Words\\Context Menu*, and in a *VB.NET* folder next to it.

### Remove or Replace a Menu

The event handler decides whether a menu appears at all and which one:

- **No menu.** Set `e.Cancel` to `true`. The following handler shows no menu in the page margins:

```csharp
private void TextControl1_TextContextMenuOpening(object sender, TextContextMenuEventArgs e)
{
    // Suppress the menu of the top and the bottom page margin
    if ((e.ContextMenuLocation & ContextMenuLocation.PageMargin) != 0)
    {
        e.Cancel = true;
    }
}
```

1. **Your own menu.** Assign a `ContextMenuStrip` of your own to `e.TextContextMenu`, for example one that you designed on the form. The reference states that the menu can be altered, extended or completely replaced.

2. **Remove items.** Remove entries from <code>e.TextContextMenu.Items</code> before the menu opens.

Setting `e.TextContextMenu` to `null` doesn't suppress the menu: the reference states that the `TextControl` then uses system menu items. Use `e.Cancel` to show no menu.

### The Menu of Misspelled Words

When a spell checker such as TX Spell .NET is connected through the [SpellChecker](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/spellchecker-property/llms-full.txt) property, a right-click on a misspelled word opens a spelling menu. The [SpellCheckContextMenuStrip](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/spellcheckcontextmenustrip-property/llms-full.txt) property of the `TextControl` chooses that menu. It is a string:

| Value | Menu |
|---|---|
| `(Default)` | The built-in spelling menu of TX Text Control |
| `(none)` | No menu |
| The name of a `ContextMenuStrip` | Your own menu |

[Spell Checking](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/spell-checking/llms-full.txt) connects the spell checker, and the TX Spell .NET page [Context Menus](https://docs.textcontrol.com/tx-spell-dotnet-winforms/user-interface/context-menus/llms-full.txt) builds a spelling menu of your own.

### The Menu of Drawings

A drawing is edited in a [TXDrawingControl](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-drawing/txdrawingcontrol/llms-full.txt), whose default context menu is a [ShapesContextMenuStrip](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-drawing/shapescontextmenustrip/llms-full.txt): Cut, Copy, Paste, Bring to Front, Send to Back, Flip and settings such as Movable and Sizable for the selected shapes. [Text Frames and Shapes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/text-frames-shapes/llms-full.txt) describes drawings.

### Context Menus and the Mini Toolbar

A right-click on a selection or an object can also show the mini toolbar, a small toolbar with common formatting commands, when [ShowMiniToolbar](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/showminitoolbar-property/llms-full.txt) enables it. The screenshot above shows both. [Toolbars and Mini Toolbars](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/toolbars/llms-full.txt) describes how to configure it.

### Next Steps

- [Built-in Dialog Boxes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/dialog-boxes/llms-full.txt): the dialog boxes your menu items can open.
- [Custom Tabs and Commands](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/custom-tabs/llms-full.txt): add the same commands to the ribbon.
- [Right-to-Left UI](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/right-to-left/llms-full.txt): menus and dialog boxes in a right-to-left application.
- [Events](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/events/llms-full.txt): the other events of the `TextControl`.

---

## Nearby

- **Previous:** [Sidebar and Panels](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/sidebar/llms-full.txt)
- **Next:** [Built-In Dialog Boxes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/dialog-boxes/llms-full.txt)
