Back to Devexpress

How to: Customize and Hide the Popup Menu

windowsforms-119050-controls-and-libraries-rich-text-editor-examples-ui-customization-how-to-customize-and-hide-the-popup-menu.md

latest5.8 KB
Original Source

How to: Customize and Hide the Popup Menu

  • Jan 21, 2025
  • 3 minutes to read

Customize the Pop-up Menu

This example demonstrates how to handle the RichEditControl.PopupMenuShowing event to customize the RichEditControl‘s context menu. You can remove and disable the existing items, and add new ones using the RichEdit commands.

Note

The CommandPopupMenu<T>.EnableMenuItem method does not enable the context menu item if you set the corresponding command’s ICommandUIState.Enabled property to false.

The RichEditMenuType enumeration lists all available context menu types. The following sample modifies the RichEditMenuType.TableCell menu, invoked by right-clicking the table cell.

View Example

csharp
private void richEditControl_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
{
    if ((e.MenuType & RichEditMenuType.TableCell) != 0)
    {
        // Remove the "Paste" menu item:
        e.Menu.RemoveMenuItem(RichEditCommandId.PasteSelection);

        // Disable the "Hyperlink..." menu item:
        e.Menu.DisableMenuItem(RichEditCommandId.CreateHyperlink);

        // Create a RichEdit command, which inserts an inline picture into a document:
        IRichEditCommandFactoryService service = (IRichEditCommandFactoryService)richEditControl.GetService(typeof(IRichEditCommandFactoryService));
        RichEditCommand cmd = service.CreateCommand(RichEditCommandId.InsertPicture);

        //Create a menu item for the new command:
        RichEditMenuItemCommandWinAdapter menuItemCommandAdapter = new RichEditMenuItemCommandWinAdapter(cmd);
        RichEditMenuItem menuItem = (RichEditMenuItem)menuItemCommandAdapter.CreateMenuItem(DevExpress.Utils.Menu.DXMenuItemPriority.Normal);
        menuItem.BeginGroup = true;
        e.Menu.Items.Add(menuItem);

        // Insert a new item into the Richedit popup menu and handle its click event:
        RichEditMenuItem myItem = new RichEditMenuItem("Highlight Selection", new EventHandler(MyClickHandler));
        e.Menu.Items.Add(myItem);
    }
}
vb
Private Sub richEditControl_PopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs) Handles richEditControl.PopupMenuShowing
    If (e.MenuType And RichEditMenuType.TableCell) <> 0 Then
        ' Remove the "Paste" menu item:
        e.Menu.RemoveMenuItem(RichEditCommandId.PasteSelection)

        ' Disable the "Hyperlink..." menu item:
        e.Menu.DisableMenuItem(RichEditCommandId.CreateHyperlink)

        ' Create a RichEdit command, which inserts an inline picture into a document:
        Dim service As IRichEditCommandFactoryService = DirectCast(richEditControl.GetService(GetType(IRichEditCommandFactoryService)), IRichEditCommandFactoryService)
        Dim cmd As RichEditCommand = service.CreateCommand(RichEditCommandId.InsertPicture)

        'Create a menu item for the new command:
        Dim menuItemCommandAdapter As New RichEditMenuItemCommandWinAdapter(cmd)
        Dim menuItem As RichEditMenuItem = CType(menuItemCommandAdapter.CreateMenuItem(DevExpress.Utils.Menu.DXMenuItemPriority.Normal), RichEditMenuItem)
        menuItem.BeginGroup = True
        e.Menu.Items.Add(menuItem)

        ' Insert a new item into the Richedit popup menu and handle its click event:
        Dim myItem As New RichEditMenuItem("Highlight Selection", New EventHandler(AddressOf MyClickHandler))
        e.Menu.Items.Add(myItem)
    End If
End Sub

Handle the PopupMenuShowing event in code after the InitializeComponents method call to obtain and customize the AI Assistant item:

csharp
public Form1() {
    InitializeComponent();
    this.richEditControl1.PopupMenuShowing += richEditControl1_PopupMenuShowing;
}
private void richEditControl1_PopupMenuShowing(object sender, DevExpress.XtraRichEdit.PopupMenuShowingEventArgs e) {
    var item = e.Menu.Items[AIIntegrationLocalizer.GetString(AIIntegrationStringId.MenuItemAIAssistant)];
    if (item != null) {
        item.Caption = "My AI Helper";
        item.ImageOptions.SvgImage = this.IconOptions.SvgImage;
    }
}
vb
Public Sub New()
    InitializeComponent()
    AddHandler Me.richEditControl1.PopupMenuShowing, AddressOf richEditControl1_PopupMenuShowing
End Sub

Private Sub richEditControl1_PopupMenuShowing(ByVal sender As Object, ByVal e As DevExpress.XtraRichEdit.PopupMenuShowingEventArgs)
    Dim item = e.Menu.Items(AIIntegrationLocalizer.GetString(AIIntegrationStringId.MenuItemAIAssistant))
    If item IsNot Nothing Then
        item.Caption = "My AI Helper"
        item.ImageOptions.SvgImage = Me.IconOptions.SvgImage
    End If
End Sub

Hide the Context Menu

Set the RichEditBehaviorOptions.ShowPopupMenu property to restrict showing the context menu, as shown below:

csharp
richEditControl.Options.Behavior.ShowPopupMenu = DocumentCapability.Disabled;
vb
richEditControl.Options.Behavior.ShowPopupMenu = DocumentCapability.Disabled