.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

IRichEditCommandFactoryService.CreateCommand(RichEditCommandId) Method

Creates a command by its ID.

Namespace: DevExpress.XtraRichEdit.Services

Assembly: DevExpress.RichEdit.v20.2.Core.dll


RichEditCommand CreateCommand(
    RichEditCommandId id
Function CreateCommand(
    id As RichEditCommandId
) As RichEditCommand


Name Type Description
id RichEditCommandId

A RichEditCommandId member specifying a command to create.


Type Description

An XtraRichEdit command object.


Implement the CreateCommand method in the custom service which substitutes the default command factory service to replace a built-in command in the XtraRichEdit.


This sample code enables you to substitute a default SaveDocumentCommand with a CustomSaveFileCommand class object, providing additional functionality.

Public NotInheritable Class MyFactoryHelper

    Private Sub New()
    End Sub

    Public Shared Sub SetMyNewCommandFactory(ByVal control As RichEditControl)
        Dim myCommandFactory = New CustomRichEditCommandFactoryService(control, control.GetService(Of IRichEditCommandFactoryService)())
        control.ReplaceService(Of IRichEditCommandFactoryService)(myCommandFactory)
    End Sub
End Class

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)
        Me.control = control
        Me.service = service
    End Sub
    Public Function CreateCommand(ByVal id As RichEditCommandId) As RichEditCommand Implements IRichEditCommandFactoryService.CreateCommand
        If id = 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 control As IRichEditControl)
    End Sub
    Protected Overrides Sub UpdateUIStateCore(ByVal state As ICommandUIState)
        ' Disable the command if the document has less than 5 paragraphs.
        state.Enabled = Control.Document.Paragraphs.Count > 5
    End Sub

    Protected Overrides Sub ExecuteCore()
        ' Cancel execution if the document contains less than 7 paragraphs.
        If Control.Document.Paragraphs.Count > 7 Then
            MessageBox.Show("You should type at least 7 paragraphs to be able to save the document.", "Please be creative", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
End Class
See Also