This example illustrates the technique used to modify the functionality of existing RichEdit commands.
Implementation Details
The RichEditControl exposes the IRichEditCommandFactoryService interface that enables you to substitute the default command with a custom command.
- Create a command class, inherited from the command that you've decided to replace. Override its methods. The main functionality and command specifics is located in the Execute or the
ExecuteCore
method (the latter does not check for the command availability). - Create a IRichEditCommandFactoryService implementation. Override the CreateCommand method to create an instance of a custom command class if an identifier of a certain command is passed as a parameter. So, instead of the default command, a custom command is used by the RichEditControl.
Finally we use this class to substitute the default RichEditControl's service.
Files to Review
Documentation
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraRichEdit.Services;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.Commands;
using DevExpress.Utils;
using DevExpress.Utils.Commands;
namespace CustomCommand
{
public partial class Form1 : Form
{
public Form1() {
InitializeComponent();
MyFactoryHelper.SetMyNewCommandFactory(this.richEditControl1);
richEditControl1.CreateNewDocument();
richEditControl1.Document.AppendSingleLineText("Type at least 7 paragraphs to be able to save the document.");
}
}
#region #iricheditcommandfactoryservice
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);
}
}
#endregion #iricheditcommandfactoryservice
}