TX Text Control .NET for Windows Forms

Context Menus

Topic

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.

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:

MemberPurpose
ContextMenuLocationThe part of the document the menu is opened for, as a combination of ContextMenuLocation values
TextContextMenuThe menu that will be shown, a System.Windows.Forms.ContextMenuStrip. You can change its items or assign another menu
LocationThe position of the menu in pixels, relative to the top-left corner of the screen
CancelSet 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:

ValueThe menu opens for
TextSelectionA text selection
TextInputPositionThe text input position
Header, NoHeaderThe top page margin with or without a header
Footer, NoFooterThe bottom page margin with or without a footer
PageMarginThe top or the bottom page margin
PageNumberFieldA page number field
SelectedFrameA selected frame: a text frame, an image, a chart or a barcode
TableA selection or the input position in a table
TextFieldA selection or the input position in a text field; the reference lists this value as Field, which does not compile
MisspelledWordA misspelled word
TableOfContentsA table of contents
TrackedChangeA tracked change
CommentCommented text
FormFieldA form field
FootnoteA 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 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 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 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.

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:

    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 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.

  • Remove items. Remove entries from e.TextContextMenu.Items 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 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:

ValueMenu
(Default)The built-in spelling menu of TX Text Control
(none)No menu
The name of a ContextMenuStripYour 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