Context Menu
The RadRichTextEditor UI has a built-in context menu feature which can be used to easily customize different elements in a document. The menu is displayed when you mouse right click on the RadRichTextEditor control. It contains some context specific commands arranged in groups. There are groups for spellchecking, clipboard, table editing and text editing.
The context menu is enabled by default. You can control this with the IsContextMenuEnabled property. If you set the property to false, the Selection Mini Tool Bar will be displayed when you click the mouse right button.
Disabling the context menu
private void DisableContextMenu()
{
this.radRichTextEditor1.IsContextMenuEnabled = false;
}
Private Sub DisableContextMenu()
Me.RadRichTextEditor1.IsContextMenuEnabled = False
End Sub
The menu is accessible through the ContextMenu property of RadRichTextEditor control.
The context menu instance is cached and shared between all the instances of RadRichTextEditor in an application.
The RadRichTextEditor default context menu can be fully replaced by an object that implements the IContextMenu interface which is marked with CustomContextMenuAttribute. Additionally, the menu can be customized by adding, removing and modifying menu groups and items. You can do that by using the Showing event of the menu or by creating a custom content builder and override its construction methods.
You can customize the context menu bu using one of the following approaches:
Using the Showing event
The first one involves subscribing to the Showing event of the default ContextMenu. The Showing event is not part of the IContextMenu interface, so in order to subscribe to it you need a cast to the ContextMenu class (the default implementation of IContextMenu in Telerik.Windows.Documents.RadRichTextEditor.dll). Here is an example of this approach.
Subscribe to Event
Telerik.WinControls.RichTextEditor.UI.ContextMenu contextMenu = (Telerik.WinControls.RichTextEditor.UI.ContextMenu)this.radRichTextEditor1.RichTextBoxElement.ContextMenu;
contextMenu.Showing += this.ContextMenu_Showing;
Dim contextMenu As Telerik.WinControls.RichTextEditor.UI.ContextMenu = DirectCast(Me.RadRichTextEditor1.RichTextBoxElement.ContextMenu, Telerik.WinControls.RichTextEditor.UI.ContextMenu)
AddHandler contextMenu.Showing, AddressOf Me.ContextMenu_Showing
Handle Event
private void ContextMenu_Showing(object sender, Telerik.WinForms.RichTextEditor.RichTextBoxUI.Menus.ContextMenuEventArgs e)
{
if (!this.radRichTextEditor1.Document.Selection.IsEmpty)
{
RadMenuItem menuItem = new RadMenuItem()
{
Text = "Change selection foreground"
};
menuItem.Click += this.OnChangeSelectionForeground;
ContextMenuGroup contextMenuGroup = new ContextMenuGroup();
contextMenuGroup.Add(menuItem);
e.ContextMenuGroupCollection.Add(contextMenuGroup);
}
}
private void OnChangeSelectionForeground(object sender, EventArgs e)
{
this.radRichTextEditor1.ChangeTextForeColor(Colors.Red);
}
Private Sub ContextMenu_Showing(sender As Object, e As Telerik.WinForms.RichTextEditor.RichTextBoxUI.Menus.ContextMenuEventArgs)
If Not Me.RadRichTextEditor1.Document.Selection.IsEmpty Then
Dim menuItem As New RadMenuItem() With {
.Text = "Change selection foreground"
}
AddHandler menuItem.Click, AddressOf Me.OnChangeSelectionForeground
Dim contextMenuGroup As New ContextMenuGroup()
contextMenuGroup.Add(menuItem)
e.ContextMenuGroupCollection.Add(contextMenuGroup)
End If
End Sub
Private Sub OnChangeSelectionForeground(sender As Object, e As EventArgs)
Me.RadRichTextEditor1.ChangeTextForeColor(Colors.Red)
End Sub
Creating ContextMenuBuilder Class
The second approach is more suitable when you need to reuse the customization across several RadRichTextEditor instances/applications. Here you can either implement the IContextMenuContentBuilder interface or derive from the ContextMenuContentBuilder class and override some of its protected methods which are responsible for the creation of each context menu group:
Custom Builder Class
public class CustomContextMenuContentBuilder : ContextMenuContentBuilder
{
public override ContextMenuGroupCollection Construct()
{
var groupsCollection = base.Construct();
if (!this.RadRichTextBox.Document.Selection.IsEmpty)
{
RadMenuItem menuItem = new RadMenuItem()
{
Text = "Change selection foreground"
};
menuItem.Click += this.OnChangeSelectionForeground;
ContextMenuGroup contextMenuGroup = new ContextMenuGroup();
contextMenuGroup.Add(menuItem);
groupsCollection.Add(contextMenuGroup);
}
return groupsCollection;
}
private void OnChangeSelectionForeground(object sender, EventArgs e)
{
this.RadRichTextBox.ChangeTextForeColor(Colors.Red);
}
}
Public Class CustomContextMenuContentBuilder
Inherits ContextMenuContentBuilder
Public Overrides Function Construct() As ContextMenuGroupCollection
Dim groupsCollection = MyBase.Construct()
If Not Me.RadRichTextBox.Document.Selection.IsEmpty Then
Dim menuItem As New RadMenuItem() With {
.Text = "Change selection foreground"
}
AddHandler menuItem.Click, AddressOf Me.OnChangeSelectionForeground
Dim contextMenuGroup As New ContextMenuGroup()
contextMenuGroup.Add(menuItem)
groupsCollection.Add(contextMenuGroup)
End If
Return groupsCollection
End Function
Private Sub OnChangeSelectionForeground(sender As Object, e As EventArgs)
Me.RadRichTextBox.ChangeTextForeColor(Colors.Red)
End Sub
End Class
Now you can simply assign the instance of your class to the ContentBuilder property of the context menu:
Assinging Builder
Telerik.WinControls.RichTextEditor.UI.ContextMenu contextMenu = (Telerik.WinControls.RichTextEditor.UI.ContextMenu)this.radRichTextEditor1.RichTextBoxElement.ContextMenu;
contextMenu.ContentBuilder = new CustomContextMenuContentBuilder();
Dim contextMenu As Telerik.WinControls.RichTextEditor.UI.ContextMenu = DirectCast(Me.RadRichTextEditor1.RichTextBoxElement.ContextMenu, Telerik.WinControls.RichTextEditor.UI.ContextMenu)
contextMenu.ContentBuilder = New CustomContextMenuContentBuilder()
And of course, for those of you who don’t need additional UI pop-ups, these can be disabled by setting the IsContextMenuEnabled property of the RadRichTextEditor to False.