Context Menus
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:

Immediately before a built-in menu opens, the TextControl raises the TextContextMenuOpening event. Its TextContextMenuEventArgs tell you where the menu opens and give you the menu itself:
| Member | Purpose |
|---|---|
| ContextMenuLocation | The part of the document the menu is opened for, as a combination of ContextMenuLocation values |
| TextContextMenu | The menu that will be shown, a System.Windows.Forms.ContextMenuStrip. You can change its items or assign another menu |
| Location | 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:
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
TextContextMenuOpeningafterInitializeComponenthas created the control. ContextMenuLocation.Tableis tested with&, because the value can be combined with others such asTextSelection.e.TextContextMenu.Items.Addappends the new items to the menu before it is shown.- TableFormatDialog opens the built-in table dialog box; Built-in Dialog Boxes 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 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.Canceltotrue. The following handler shows no menu in the page margins: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; } }Your own menu. Assign a
ContextMenuStripof your own toe.TextContextMenu, for example one that you designed on the form. The reference states that the menu can be altered, extended or completely replaced.Remove items. Remove entries from
e.TextContextMenu.Itemsbefore 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 property, a right-click on a misspelled word opens a spelling menu. The SpellCheckContextMenuStrip 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 connects the spell checker, and the TX Spell .NET page Context Menus builds a spelling menu of your own.
The Menu of Drawings
A drawing is edited in a TXDrawingControl, whose default context menu is a ShapesContextMenuStrip: 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 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 enables it. The screenshot above shows both. Toolbars and Mini Toolbars describes how to configure it.
Next Steps
- Built-in Dialog Boxes: the dialog boxes your menu items can open.
- Custom Tabs and Commands: add the same commands to the ribbon.
- Right-to-Left UI: menus and dialog boxes in a right-to-left application.
- Events: the other events of the
TextControl.