Back to Devexpress

SaveDocumentAsCommand Class

officefileapi-devexpress-dot-xtrarichedit-dot-commands-8f17009d.md

latest7.4 KB
Original Source

SaveDocumentAsCommand Class

Invokes the File dialog that prompts for a file name, and saves a document in a file with the specified name and format.

Namespace : DevExpress.XtraRichEdit.Commands

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

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
public class SaveDocumentAsCommand :
    RichEditMenuItemSimpleCommand
vb
Public Class SaveDocumentAsCommand
    Inherits RichEditMenuItemSimpleCommand

Remarks

To adjust the SaveAs dialog settings or to accomplish other specific tasks you can substitute the default command with your own custom descendant.

A custom command inherits from the SaveDocumentAsCommand class and overrides the protected ExecuteCore method. To substitute a default command with a custom command, create a new command factory - the class that implements the IRichEditCommandFactoryService interface and intercepts a call that creates a SaveDocumentAsCommand command. Register the new command factory via the IRichEditDocumentServer.ReplaceService<T> method.

The technique is illustrated in the following code snippet.

View Example

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.Export;
using DevExpress.XtraRichEdit.Services;

public Form1() {
    InitializeComponent();

    CustomRichEditCommandFactoryService commandFactory = 
        new CustomRichEditCommandFactoryService(richEditControl1, 
            richEditControl1.GetService<IRichEditCommandFactoryService>());
    richEditControl1.ReplaceService<IRichEditCommandFactoryService>(commandFactory);
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.Export
Imports DevExpress.XtraRichEdit.Services

Public Sub New()
    InitializeComponent()

    Dim commandFactory As New CustomRichEditCommandFactoryService(richEditControl1, richEditControl1.GetService(Of IRichEditCommandFactoryService)())
    richEditControl1.ReplaceService(Of IRichEditCommandFactoryService)(commandFactory)
End Sub
csharp
public class CustomRichEditCommandFactoryService : IRichEditCommandFactoryService
{
    readonly IRichEditCommandFactoryService service;
    readonly RichEditControl control;
    public CustomRichEditCommandFactoryService(RichEditControl control, IRichEditCommandFactoryService service)
    {
        DevExpress.Utils.Guard.ArgumentNotNull(control, "control");
        DevExpress.Utils.Guard.ArgumentNotNull(service, "service");
        this.control = control;
        this.service = service;
    }
    public RichEditCommand CreateCommand(RichEditCommandId id)
    {
        if (id == RichEditCommandId.FileSaveAs)
            return new CustomSaveDocumentAsCommand(control);

        return service.CreateCommand(id);
    }
}

public class CustomSaveDocumentAsCommand : SaveDocumentAsCommand
{
    public CustomSaveDocumentAsCommand(IRichEditControl control)
        : base(control) {}

    protected override void ExecuteCore()
    {
        SaveFileDialog dialog = new SaveFileDialog
        {
            Filter = "Rich Text Format Files (*.rtf)|*.rtf|All Files (*.*)|*.*",
            FileName = "SavedDocument.rtf",
            RestoreDirectory = true,
            CheckFileExists = false,
            CheckPathExists = true,
            OverwritePrompt = true,
            DereferenceLinks = true,
            ValidateNames = true,
            AddExtension = false,
            FilterIndex = 1
        };
        dialog.InitialDirectory = "C:\\Temp";
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            ((RichEditControl)this.Control).SaveDocument(dialog.FileName, DocumentFormat.Rtf);
        }
        //base.ExecuteCore();
    }
}
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 = RichEditCommandId.FileSaveAs Then
            Return New CustomSaveDocumentAsCommand(control)
        End If

        Return service.CreateCommand(id)
    End Function
End Class

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

    Protected Overrides Sub ExecuteCore()
        Dim dialog As SaveFileDialog = New SaveFileDialog With {.Filter = "Rich Text Format Files (*.rtf)|*.rtf|All Files (*.*)|*.*", .FileName = "SavedDocument.rtf", .RestoreDirectory = True, .CheckFileExists = False, .CheckPathExists = True, .OverwritePrompt = True, .DereferenceLinks = True, .ValidateNames = True, .AddExtension = False, .FilterIndex = 1}
        dialog.InitialDirectory = "C:\Temp"
        If dialog.ShowDialog() = DialogResult.OK Then
            CType(Me.Control, RichEditControl).SaveDocument(dialog.FileName, DocumentFormat.Rtf)
        End If
        'base.ExecuteCore();
    End Sub
End Class

You can also use the RichEditControl.SaveDocumentAs API method to achieve the same result.

Inheritance

Object Command DevExpress.Utils.Commands.ControlCommand<IRichEditControl, RichEditCommandId, DevExpress.XtraRichEdit.Localization.XtraRichEditStringId> DevExpress.XtraRichEdit.Commands.RichEditCommandBase<DevExpress.XtraRichEdit.Localization.XtraRichEditStringId> RichEditCommand RichEditMenuItemSimpleCommand SaveDocumentAsCommand

See Also

SaveDocumentAsCommand Members

SaveDocumentCommand

Keyboard Shortcuts in Rich Text Editor

DevExpress.XtraRichEdit.Commands Namespace