New to Telerik UI for WinForms? Download free 30-day trial

Undo and Redo

Telerik Diagramming Framework exposes Undo/Redo functionality. The framework allows you to keep track of the changes made in a Diagramming structure and trigger an undo or redo action using commands, methods or keyboard shortcuts.

Undo/Redo methods

RadDiagram class exposes two methods that allow you to take advantage of the undo/redo functionality.

  • Undo(): this method reverts the last operation in the RadDiagram.

  • Redo(): this method reapplies the last operation that was undone in the RadDiagram.


private void radButtonUndo_Click(object sender, EventArgs e)
{
    this.radDiagram1.Undo();
}

private void radButtonRedo_Click(object sender, EventArgs e)
{
    this.radDiagram1.Redo();
}

Private Sub RadButtonUndo_Click(sender As Object, e As EventArgs) Handles RadButtonUndo.Click
    Me.RadDiagram1.Undo()
End Sub
Private Sub RadButtonRedo_Click(sender As Object, e As EventArgs) Handles RadButtonRedo.Click
    Me.RadDiagram1.Redo()
End Sub

Figure 1: Undo/Redo

WinForms RadDiagram Undo/Redo

Undo/Redo commands

Telerik Diagramming Framework exposes a set of commands that allow you to easily implement RadDiagram interaction logic in MVVM solutions. In order to trigger the Undo/Redo functionality you can use the DiagramCommands.Undo and DiagramCommands.Redo commands. Please keep in mind that in order to use those commands for reverting back and forward RadDiagram operations, you need to explicitly set the diagram as the target of the commands.

For a full list of the DiagramCommands , please refer to the Commands tutorial.

this.radDiagram1.DiagramElement.TryExecuteCommand(Telerik.WinControls.UI.Diagrams.DiagramCommands.Undo);
this.radDiagram1.DiagramElement.TryExecuteCommand(Telerik.WinControls.UI.Diagrams.DiagramCommands.Redo);

Me.RadDiagram1.DiagramElement.TryExecuteCommand(Telerik.WinControls.UI.Diagrams.DiagramCommands.Undo)
Me.RadDiagram1.DiagramElement.TryExecuteCommand(Telerik.WinControls.UI.Diagrams.DiagramCommands.Redo)

UndoRedoService

You can further extend the undo/redo functionality of your Diagramming solution using the RadDiagram.UndoRedoService property. It exposes the following properties:

  • RedoStack: use it to get the IEnumerable collection of redid actions.

  • RedoBufferSize: use it to get or set the redo actions buffer size.

  • UndoStack: use it to get the IEnumerable collection of undid actions.

  • UndoBufferSize: use it to get or set the undo actions buffer size.

The UndoRedoService also exposes undo/redo methods:

  • CanRedo(): this method determines if the RadDiagram instance can redo operations.

  • Redo(): this method reapplies the last operation that was undone in the RadDiagram.

  • CanUndo(): this method determines if the RadDiagram instance can undo operations.

  • Undo(): this method reverts the last operation in the RadDiagram.

  • ExecuteCommand(): this method allows you to execute a custom command. It takes as an argument a Telerik.Windows.Diagrams.Core.ICommand command and it also allows you to pass a state parameter as its second argument.

Keyboard support

You can also trigger an undo or redo action using a keyboard combination:

  • Ctrl+Z: this key combination will trigger an Undo action.

  • Ctrl+Y: this key combination will trigger a Redo action.

In this article