Skip to main content

IRichEditCommandFactoryService.CreateCommand(RichEditCommandId) Method

Creates a command by its ID.

Namespace: DevExpress.XtraRichEdit.Services

Assembly: DevExpress.RichEdit.v26.1.Core.dll

NuGet Package: DevExpress.RichEdit.Core

Declaration

RichEditCommand CreateCommand(
    RichEditCommandId id
)

Parameters

Name Type Description
id RichEditCommandId

A RichEditCommandId member specifying a command to create.

Returns

Type Description
RichEditCommand

An XtraRichEdit command object.

Remarks

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

Example

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

View Example

public static class MyFactoryHelper {
    public static void SetMyNewCommandFactory(RichEditControl control) {
        var myCommandFactory = new CustomRichEditCommandFactoryService(control, control.GetService<IRichEditCommandFactoryService>());
        control.ReplaceService<IRichEditCommandFactoryService>(myCommandFactory);
    }
}

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

    public CustomRichEditCommandFactoryService(RichEditControl control,
        IRichEditCommandFactoryService service) {
        this.control = control;
        this.service = service;
    }
    public RichEditCommand CreateCommand(RichEditCommandId id) {
        if (id == RichEditCommandId.FileSave)
            return new CustomSaveDocumentCommand(control);
        return service.CreateCommand(id);
    }
}

public class CustomSaveDocumentCommand : SaveDocumentCommand {
    public CustomSaveDocumentCommand(IRichEditControl control)
        : base(control) {
    }
    protected override void UpdateUIStateCore(ICommandUIState state) {
        base.UpdateUIStateCore(state);
        // Disable the command if the document has less than 5 paragraphs.
        state.Enabled = Control.Document.Paragraphs.Count > 5;
    }

    protected override void ExecuteCore() {
        // Cancel execution if the document contains less than 7 paragraphs.
        if (Control.Document.Paragraphs.Count > 7)
            base.ExecuteCore();
        else MessageBox.Show(@"You should type at least 7 paragraphs to be able to save the document.",
            "Please be creative", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}
See Also