Skip to main content
A newer version of this page is available. .

Commands in Rich Text Editor

  • 3 minutes to read

Any user action performed with the RichEdit control results in a command object being created and executed. All commands implemented in the Rich Edit control inherit from the Command object. Virtually every task can be accomplished via commands, such as inserting characters and pictures, selection, formatting, creating numbered lists, scrolling, etc.

The command’s main method is Command.Execute. It accomplishes the task for which the command is implemented. Other properties, such as Command.Image, Command.LargeImage, Command.Description and Command.MenuCaption are used to visually and verbally represent a command. The following table shows the InsertTableRowAboveCommand command properties as an example.

Command Large image Small image Menu caption Description
InsertTableRowAboveCommand Command_insert_table_rows_above_large Command_insert_table_rows_above_small Insert Rows Above Add a new row directly above the selected row

If the command is disabled for any reason, you can still use its Command.ForceExecute method to execute it. A command may require additional data for its execution. Usually they are provided by the ICommandUIState.EditValue property.

The following code creates commands by their RichEditCommandId using the RichEditControl.CreateCommand method. Each command is executed by calling the ForceExecute method. The Command.CreateDefaultCommandUIState method returns the command UI state.

The RichEditControl instance is passed to the BarItem.ItemClick event handler using the BarItem.Tag property.

View Example

static void buttonCustomAction_ItemClick_Commands(object sender, ItemClickEventArgs e) {
    RichEditControl richEdit = e.Item.Tag as RichEditControl;
    richEdit.SelectAll();

    RichEditCommand capCommand = richEdit.CreateCommand(RichEditCommandId.CapitalizeEachWordTextCase);
    capCommand.ForceExecute(capCommand.CreateDefaultCommandUIState());

    RichEditCommand boldCommand = richEdit.CreateCommand(RichEditCommandId.ToggleFontBold);
    boldCommand.ForceExecute(boldCommand.CreateDefaultCommandUIState());

    RichEditCommand changeFontColorCommand = richEdit.CreateCommand(RichEditCommandId.ChangeFontBackColor);
    DevExpress.Utils.Commands.ICommandUIState state = changeFontColorCommand.CreateDefaultCommandUIState();
    state.EditValue = Color.Yellow;
    changeFontColorCommand.ForceExecute(state);

    RichEditCommand previewCommand = richEdit.CreateCommand(RichEditCommandId.PrintPreview);
    previewCommand.ForceExecute(previewCommand.CreateDefaultCommandUIState());

    richEdit.DeselectAll();
}

You can modify the default functionality of virtually any command by substituting the RichEditCommandFactoryService, available via the IRichEditCommandFactoryService interface. See the IRichEditCommandFactoryService.CreateCommand topic for more information.

Most commands can be executed via keyboard shortcuts recognized by RichEditControl. A list of default command shortcuts is shown in the Keyboard Shortcuts document. You can use the RichEditControl.RemoveShortcutKey and the RichEditControl.AssignShortcutKeyToCommand methods to modify shortcut keys assigned to commands.

You can use commands to replicate user activity on another RichEdit control, even on a different platform.

You can bind a command to virtually any UI element. This technique is illustrated in the How to: Bind a Command to a Button and How to: Customize and Hide the Popup Menu documents.

Note

You should always create a new instance of a command before execution, otherwise you may experience undesired effects and unhandled exceptions.

Note

Commands executed via the Bar (Ribbon) user interface can throw unhandled exceptions if a problem occurs. Consider the situation when a document is being saved to a locked or read-only file. To prevent application failure, subscribe to the RichEditControl.UnhandledException event and set the RichEditUnhandledExceptionEventArgs.Handled property to true.