RadRichTextEditor - Modify Context Menu

2 Answers 127 Views
ContextMenu RichTextEditor
Amanda
Top achievements
Rank 1
Iron
Amanda asked on 04 Aug 2022, 05:34 PM

Hello all!

I'm working with a use case where I can't allow the user to edit an inserted image - we're not saving the bytes, just the URL the image comes from, so the user editing the image would just be wasted time.

I've disabled the 'CanDrag', 'CanResize', and 'CanRotate' on the ImageSelectinoAdornerSettings layer. I even tried removing the layer per a forum post I found - however if I right click on the image, I see get a functional 'Edit Image' option on the context menu.

I've looked at the RadRichTextEditor Context Menu article (https://docs.telerik.com/devtools/winforms/controls/richtexteditor/ui-for-applying-rich-text-formatting/context-menu), but it doesn't show how to loop through the groups and/or the menu items themselves, and I'm hitting a wall when I try to figure that out myself.

Any suggestions on how to remove the Edit Image context menu item?

Thank you!

2 Answers, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 05 Aug 2022, 08:43 AM

Hello, Amanda,   

I am glad that you have found a suitable solution for your scenario. 

Please have in mind that RadRichTextEditor internally uses a ContextMenuContentBuilder which is responsible for populating the menu items according to the currently selected element in the document. I can suggest an alternative solution with a custom ContextMenuContentBuilder. It allows overriding multiple methods that are responsible for creating and populating the different groups of items.

        public RadForm1()
        {
            InitializeComponent();

            Telerik.WinControls.RichTextEditor.UI.ContextMenu contextMenu = (Telerik.WinControls.RichTextEditor.UI.ContextMenu)this.radRichTextEditor1.RichTextBoxElement.ContextMenu;
            contextMenu.ContentBuilder = new CustomContextMenuContentBuilder();
        } 

        public class CustomContextMenuContentBuilder : ContextMenuContentBuilder
        {
            protected override ContextMenuGroup CreateImageCommands()
            {
                //return base.CreateImageCommands();
                return null;
            }

            protected override ContextMenuGroup CreateFloatingBlockCommands()
            {
                //return base.CreateFloatingBlockCommands();
                return null;
            }
        }
    }

Before:

After:

Feel free to use this approach which suits your requirements best.

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Amanda
Top achievements
Rank 1
Iron
commented on 05 Aug 2022, 04:56 PM

Thank you Dess for offering this alternative! I will try it out and see which works best for us!
0
Amanda
Top achievements
Rank 1
Iron
answered on 04 Aug 2022, 11:12 PM

I have figured out how to remove the groups I needed! Ended up being two - the ImageCommands and the FloatingBlockCommands.

First, when I initialize my form, I hook the ContextMenu Showing Event to a custom paragraph I'm going to show below:

Telerik.WinControls.RichTextEditor.UI.ContextMenu contextMenu = (Telerik.WinControls.RichTextEditor.UI.ContextMenu)rtfDescription.RichTextBoxElement.ContextMenu;
contextMenu.Showing += ContextMenu_Showing;

Then, in ContextMenu_Showing, I make two new variables to put the ImageCommands and FloatingBlockCommands in so I can loop through the entire collection without the list being changed, then afterwards I simply remove them!

private void ContextMenu_Showing(object sender, ContextMenuEventArgs e)
{
    ContextMenuGroup delImage = new ContextMenuGroup();
    ContextMenuGroup delFloating = new ContextMenuGroup();

    foreach (ContextMenuGroup contextGroup in e.ContextMenuGroupCollection)
    {
        if (contextGroup.Type.ToString() == "ImageCommands")
        {
            delImage = contextGroup;
        }

        if (contextGroup.Type.ToString() == "FloatingBlockCommands")
        {
            delFloating = contextGroup;
        }
    }

    e.ContextMenuGroupCollection.Remove(delImage);
    e.ContextMenuGroupCollection.Remove(delFloating);
}

Tags
ContextMenu RichTextEditor
Asked by
Amanda
Top achievements
Rank 1
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Amanda
Top achievements
Rank 1
Iron
Share this question
or