Example E2224
Visible to All Users

Rich Text Editor for WinForms - How to Replace Standard Command with Custom Command

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.

  1. 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).
  2. 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

CustomCommand/Form1.cs(vb)
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 }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.