Replacing the editor's right-click context menu
The WinForms HTML Editor's default right-click context menu covers general editing commands: cut, copy, paste, table commands, image properties. Your app may need more than that - a CRM note editor that lets a rep link the current selection to a support ticket, or a ticketing app that turns selected text into a task, right from the same right-click gesture users already reach for. Replace the default menu with your own ContextMenuStrip when your document needs commands like these instead of, or alongside, the built-in ones. This page shows how to swap the menu, enable or disable items based on the current selection, and observe right-clicks without touching the built-in menu at all.
Replace the menu via EditorContextMenuStrip
The editor exposes a single property for this: EditorContextMenuStrip, of type System.Windows.Forms.ContextMenuStrip. Assign your own strip and the editor uses it whenever the user right-clicks inside the document surface.
private void NoteEditorForm_Load(object sender, EventArgs e) { var menu = new ContextMenuStrip(); var linkToTicket = new ToolStripMenuItem("Link to Ticket..."); linkToTicket.Click += (s, args) => { var ticketId = TicketPicker.Show(this); if (ticketId != null) { string anchor = $"<a href=\"crm://ticket/{ticketId}\">#{ticketId}</a>"; htmlEditor1.Content.InsertHtmlAtCaret(anchor); } }; var convertToTask = new ToolStripMenuItem("Convert Selection to Task"); convertToTask.Click += (s, args) => { string selected = htmlEditor1.Selection.GetSelectedHtml(); if (!string.IsNullOrWhiteSpace(selected)) TaskService.CreateFromHtml(currentAccount.Id, selected); }; menu.Items.Add(linkToTicket); menu.Items.Add(new ToolStripSeparator()); menu.Items.Add(convertToTask); // Hand the strip to the editor. From now on, right-clicking inside the // note shows this menu instead of the built-in one. htmlEditor1.EditorContextMenuStrip = menu;}Private Sub NoteEditorForm_Load(sender As Object, e As EventArgs) Dim menu = New ContextMenuStrip() Dim linkToTicket = New ToolStripMenuItem("Link to Ticket...") linkToTicket.Click += Sub(s, args) Dim ticketId = TicketPicker.Show(Me) If ticketId IsNot Nothing Then Dim anchor As String = $"<a href=""crm://ticket/{ticketId}"">#{ticketId}</a>" htmlEditor1.Content.InsertHtmlAtCaret(anchor) End If End Sub Dim convertToTask = New ToolStripMenuItem("Convert Selection to Task") convertToTask.Click += Sub(s, args) Dim selected As String = htmlEditor1.Selection.GetSelectedHtml() If Not String.IsNullOrWhiteSpace(selected) Then TaskService.CreateFromHtml(currentAccount.Id, selected) End Sub menu.Items.Add(linkToTicket) menu.Items.Add(New ToolStripSeparator()) menu.Items.Add(convertToTask) ' Hand the strip to the editor. From now on, right-clicking inside the // note shows this menu instead of the built-in one. htmlEditor1.EditorContextMenuStrip = menu;} End Sub[IMAGE: editor-context-menu-strip.png -- right-click menu showing Link to Ticket and Convert to Task in place of the default menu]
Disable items based on current state
To disable an item conditionally, for example greying out a command when there is no selection, handle the Opening event on the EditorContextMenuStrip (a normal Windows Forms ContextMenuStrip):
menu.Opening += (s, args) => { string selected = htmlEditor1.Selection.GetSelectedHtml(); convertToTask.Enabled = !string.IsNullOrWhiteSpace(selected); };menu.Opening += Sub(s, args) Dim selected As String = htmlEditor1.Selection.GetSelectedHtml() convertToTask.Enabled = Not String.IsNullOrWhiteSpace(selected)[IMAGE: context-menu-opening-state.png -- menu with Convert to Task disabled because there is no selection]
The ContextMenuShowing event: a notification hook
To observe right-clicks without replacing the menu, for example for usage analytics, handle the ContextMenuShowing event. It fires whenever the built-in menu is about to appear, and its event args give you the cursor position relative to the editor:
htmlEditor1.ContextMenuShowing += (sender, e) => { // e.OffsetMousePosition is the cursor location relative to the editor surface. AnalyticsClient.Track( "NoteEditorRightClick", new { x = e.OffsetMousePosition.X, y = e.OffsetMousePosition.Y }); };Note: ContextMenuShowingEventArgs only carries OffsetMousePosition. There is no Cancel property and no MenuItems collection, so you cannot suppress or modify the built-in menu from this event. To show a different menu, use EditorContextMenuStrip instead; this event is for observation only.
[IMAGE: context-menu-showing-event.png -- event firing as the built-in menu appears, with the offset coordinates highlighted]
Because the strip is built once in code, you can extend it later, for example adding another ToolStripMenuItem for a new command, without changing how it is wired to the editor.