TX Words: Context Menu

When right-clicking TextControl, a built-in context menu appears that provides context related items. This sample shows how to extend such a TextControl context menu. When right-clicking a frame base element, an additional item is added to the context menu that opens a dialog to edit the name of that object.

The source code for this example is contained in the following directories:

  • %USERPROFILE%\Documents\TX Text Control 32.0.NET for WPF\CSharp\TX Words\Context Menu
  • %USERPROFILE%\Documents\TX Text Control 32.0.NET for WPF\VB.NET\TX Words\Context Menu

Using the Sample

Insert a frame base element and right-click it. Now the default context menu appears including an additional item (Frame Name...). When clicking that item, a dialog is opened to set the name of the clicked frame base element.

Image

The Code Behind

When right-clicking TextControl, a TextContextMenuOpening event is fired. Its handler has an event argument that provides the shown context menu and information about it. In this sample, when a frame base element is right-clicked, that context menu is extended by a tool strip separator and an item that opens a custom dialog to edit that object.

private void TextControl_TextContextMenuOpening(object sender, TextContextMenuEventArgs e) {
        if ((e.ContextMenuLocation & ContextMenuLocation.SelectedFrame) != 0) {
                // A frame is selected
                AddFrameContextMenuItems(e.TextContextMenu);
        }
}
Private Sub TextControl_TextContextMenuOpening(ByVal sender As Object, ByVal e As TextContextMenuEventArgs)
        If (e.ContextMenuLocation And ContextMenuLocation.SelectedFrame) <> 0 Then
                ' A frame is selected
                AddFrameContextMenuItems(e.TextContextMenu)
        End If
End Sub