Back to Devexpress

How to: Replace a Built-In Command with a Custom Command in Rich Text Editor for WinForms

windowsforms-113758-controls-and-libraries-rich-text-editor-examples-commands-how-to-customize-built-in-command-using-service-substitution.md

latest6.0 KB
Original Source

How to: Replace a Built-In Command with a Custom Command in Rich Text Editor for WinForms

  • Apr 12, 2023
  • 3 minutes to read

To change the actions performed when the built-in command is executed, use the service substitution technique. Create the IRichEditCommandFactoryService descendant which generates custom commands and use the RichEditControl.ReplaceService<T> method (or the GetService -> RemoveService -> AddService method sequence) to replace default service with a newly created service.

Create A Service Descendant

The following code implements the IRichEditCommandFactoryService descendant which generates customized SaveDocumentCommand and SaveDocumentAsCommand commands. The command descendants override the ExecuteCore methods to display a message after a document has been saved. The IsModified internal property indicates unsaved changes in the document. If this property is not changed to false after saving a document, it signals that the save action failed.

View Example

csharp
public class CustomRichEditCommandFactoryService : IRichEditCommandFactoryService
{
    readonly IRichEditCommandFactoryService service;
    readonly RichEditControl control;

    public CustomRichEditCommandFactoryService(RichEditControl control, IRichEditCommandFactoryService service)
    {
        Guard.ArgumentNotNull(control, "control");
        Guard.ArgumentNotNull(service, "service");
        this.control = control;
        this.service = service;
    }

    public RichEditCommand CreateCommand(RichEditCommandId id) {
        if (id == RichEditCommandId.FileSaveAs) {
            return new CustomSaveDocumentAsCommand(control);
        }
        if (id == RichEditCommandId.FileSave) {
            return new CustomSaveDocumentCommand(control);
        }
        return service.CreateCommand(id);
    }
}

public class CustomSaveDocumentCommand : SaveDocumentCommand {
    public CustomSaveDocumentCommand(IRichEditControl richEdit) : base(richEdit) { }
    protected override void ExecuteCore() {
        base.ExecuteCore();
        if (!DocumentServer.Modified) {
            MessageBox.Show("Document is saved successfully");
        }
    }
}

public class CustomSaveDocumentAsCommand : SaveDocumentAsCommand {
    public CustomSaveDocumentAsCommand(IRichEditControl richEdit) : base(richEdit) { }
    protected override void ExecuteCore() {
        DocumentServer.Modified = true;
        base.ExecuteCore();
        if (!DocumentServer.Modified) {
            MessageBox.Show("Document is saved successfully");
        }
    }
}
vb
Public Class CustomRichEditCommandFactoryService
    Implements IRichEditCommandFactoryService
    Private ReadOnly service As IRichEditCommandFactoryService
    Private ReadOnly control As RichEditControl

    Public Sub New(ByVal control As RichEditControl, ByVal service As IRichEditCommandFactoryService)
        DevExpress.Utils.Guard.ArgumentNotNull(control, "control")
        DevExpress.Utils.Guard.ArgumentNotNull(service, "service")
        Me.control = control
        Me.service = service
    End Sub

    Public Function CreateCommand(ByVal id As RichEditCommandId) As RichEditCommand Implements IRichEditCommandFactoryService.CreateCommand
        If id.Equals(RichEditCommandId.FileSaveAs) Then
            Return New CustomSaveDocumentAsCommand(control)
        End If
        If id.Equals(RichEditCommandId.FileSave) Then
            Return New CustomSaveDocumentCommand(control)
        End If
        Return service.CreateCommand(id)
    End Function
End Class

Public Class CustomSaveDocumentCommand
    Inherits SaveDocumentCommand
    Public Sub New(ByVal richEdit As IRichEditControl)
        MyBase.New(richEdit)
    End Sub

    Protected Overrides Sub ExecuteCore()
        MyBase.ExecuteCore()
        If (Not DocumentServer.Modified) Then
            MessageBox.Show("Document is saved successfully")
        End If
    End Sub
End Class

Public Class CustomSaveDocumentAsCommand
    Inherits SaveDocumentAsCommand
    Public Sub New(ByVal richEdit As IRichEditControl)
        MyBase.New(richEdit)
    End Sub

    Protected Overrides Sub ExecuteCore()
        DocumentServer.Modified = True
        MyBase.ExecuteCore()
        If (Not DocumentServer.Modified) Then
            MessageBox.Show("Document is saved successfully")
        End If
    End Sub
End Class

Substitute the Service

The following code replaces the IRichEditCommandFactoryService service with its customized descendant.

View Example

csharp
var builtInService = richEditControl1.GetService<IRichEditCommandFactoryService>();
var myCommandFactory = new CustomRichEditCommandFactoryService(richEditControl1,builtInService);
richEditControl1.ReplaceService<IRichEditCommandFactoryService>(myCommandFactory);
vb
Dim builtInService = richEditControl1.GetService(Of IRichEditCommandFactoryService)()
Dim myCommandFactory = New CustomRichEditCommandFactoryService(richEditControl1,builtInService)
richEditControl1.ReplaceService(Of IRichEditCommandFactoryService)(myCommandFactory)