Back to Devexpress

How to: Replace Built-In Command with a Custom Command

windowsforms-18004-controls-and-libraries-spreadsheet-examples-commands-how-to-replace-built-in-command-with-a-custom-command.md

latest5.0 KB
Original Source

How to: Replace Built-In Command with a Custom Command

  • Nov 05, 2023
  • 2 minutes to read

This example illustrates the technique used to modify the functionality of the existing SpreadsheetControl command used to clear the contents of the selected cell.

The SpreadsheetControl exposes the ISpreadsheetCommandFactoryService interface that enables you to substitute the default command with your own custom command.

You have to create a custom command class, inherited from the command that you wish to replace. In this example, it is the DevExpress.XtraSpreadsheet.Commands.FormatClearContentsCommand class. Override the ExecuteCore method to specify actions performed by a command when it is executed.

View Example

csharp
public class CustomFormatClearContentsCommand : FormatClearContentsCommand {
    public CustomFormatClearContentsCommand(ISpreadsheetControl control)
        : base(control) {
    }

    protected override void ExecuteCore() {
        MessageBox.Show("Custom command executed");
        base.ExecuteCore();
    }
}
vb
Public Class CustomFormatClearContentsCommand
    Inherits FormatClearContentsCommand

    Public Sub New(ByVal control As ISpreadsheetControl)
        MyBase.New(control)
    End Sub

    Protected Overrides Sub ExecuteCore()
        MessageBox.Show("Custom command executed")
        MyBase.ExecuteCore()
    End Sub
End Class

Create a class inherited from the SpreadsheetCommandFactoryServiceWrapper. This class will replace the default command service. In this class, you should override the CreateCommand method to create an instance of a custom command class. An identifier of the currently processed command is passed as a parameter to this method.

View Example

csharp
public class CustomService : SpreadsheetCommandFactoryServiceWrapper {
    public CustomService(ISpreadsheetCommandFactoryService service)
        : base(service) {
    }
    public SpreadsheetControl Control {get;set;}

    public override SpreadsheetCommand CreateCommand(SpreadsheetCommandId id)
    {
        if (id == SpreadsheetCommandId.FormatClearContents || id == SpreadsheetCommandId.FormatClearContentsContextMenuItem)
            return new CustomFormatClearContentsCommand(Control);

        return base.CreateCommand(id);
    }

}
vb
Public Class CustomService
    Inherits SpreadsheetCommandFactoryServiceWrapper

    Public Sub New(ByVal service As ISpreadsheetCommandFactoryService)
        MyBase.New(service)
    End Sub
    Public Property Control() As SpreadsheetControl

    Public Overrides Function CreateCommand(ByVal id As SpreadsheetCommandId) As SpreadsheetCommand
        If id = SpreadsheetCommandId.FormatClearContents OrElse id = SpreadsheetCommandId.FormatClearContentsContextMenuItem Then
            Return New CustomFormatClearContentsCommand(Control)
        End If

        Return MyBase.CreateCommand(id)
    End Function

End Class

Use the SpreadsheetCommandFactoryServiceWrapper class descendant to substitute the default command service obtained using the SpreadsheetControl.GetService<T> method.

View Example

csharp
private void SubstituteService() {
    ISpreadsheetCommandFactoryService service = (ISpreadsheetCommandFactoryService)spreadsheetControl1.GetService(typeof(ISpreadsheetCommandFactoryService));
    CustomService customService = new CustomService(service);
    spreadsheetControl1.ReplaceService<ISpreadsheetCommandFactoryService>(customService);
    customService.Control = spreadsheetControl1;
}
vb
Private Sub SubstituteService()
    Dim service As ISpreadsheetCommandFactoryService = DirectCast(spreadsheetControl1.GetService(GetType(ISpreadsheetCommandFactoryService)), ISpreadsheetCommandFactoryService)
    Dim customService As New CustomService(service)
    spreadsheetControl1.ReplaceService(Of ISpreadsheetCommandFactoryService)(customService)
    customService.Control = spreadsheetControl1
End Sub

Now, instead of clearing the contents of the selected cell, you will get the message “Custom command executed”.