Commands
- 2 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 ChangeParagraphBackColorCommand command properties as an example.
Command | Large image | Small image | Menu caption | Description |
---|---|---|---|---|
Change |
![]() |
![]() |
Shading | Change the background behind the selected paragraph. |
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 snippet demonstrates how the ICommandUIState.EditValue property should be used to execute value-based commands, such as ChangeFontBackColorCommand, ChangeFontColorCommand, ChangeFontNameCommand, ChangeFontSizeCommand, ChangeParagraphBackColorCommand.
ChangeFontBackColorCommand cmd = new ChangeFontBackColorCommand(richEditControl1);
DevExpress.Utils.Commands.IValueBasedCommandUIState<Color> state =
cmd.CreateDefaultCommandUIState()
as DevExpress.Utils.Commands.IValueBasedCommandUIState<Color>;
state.Value = Color.Yellow;
cmd.ForceExecute(state);
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. Note that keyboard shortcuts assigned to RichEditControl may conflict with the shortucts intercepted by a browser. 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. This technique is elaborated in the Enabling Collaboration with RichEdit Control document.
NOTE
You should always create a new instance of a command before execution, otherwise you may experience undesired effects and unhandled exceptions.