Skip to main content

IRichEditCommandFactoryService.CreateCommand(RichEditCommandId) Method

Creates a command by its ID.

Namespace: DevExpress.XtraRichEdit.Services

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

NuGet Packages: DevExpress.RichEdit.Core, DevExpress.Win.Navigation

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);
    }
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the CreateCommand(RichEditCommandId) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also