Undo / Redo in Windows Forms Diagram

18 Nov 20182 minutes to read

The Diagram control records user actions (such as adding, moving, resizing, or deleting nodes) into the HistoryManager. The active history manager exposes methods to undo and redo those recorded actions. Recording is enabled by default when a model is created; you can pause and resume recording with Suspend and Resume.

Before running the samples below, add a Diagram control named diagram1 to a Windows Form.

History Manager Methods

Method Description
Undo Undoes the most recent recorded action from the undo stack.
Redo Redoes the most recent undone action. Redo is only available after Undo has been called.
StartAtomicAction(string actionName) Begins an atomic (composite) action. All recorded actions between Start and End are merged into a single undo entry titled actionName.
EndAtomicAction() Ends the atomic action started by `StartAtomicAction` and resumes normal recording of new actions.

Undo and Redo

Calling Undo reverses the most recent recorded action. The action is then pushed onto the redo stack, allowing Redo to restore it on the next call.

// Reverse the most recent recorded action.
this.diagram1.Model.HistoryManager.Undo();

// Reapply the action that was just undone.
this.diagram1.Model.HistoryManager.Redo();
' Reverse the most recent recorded action.
Me.diagram1.Model.HistoryManager.Undo()

' Reapply the action that was just undone.
Me.diagram1.Model.HistoryManager.Redo()

If Redo() is called when the redo stack is empty, the call is a no-op. Redo only works after at least one Undo has been performed.

Atomic Actions

Use StartAtomicAction and EndAtomicAction to group several actions into a single undoable unit. This is useful when a custom command performs multiple changes (for example, while swapping two nodes) and the user expects Undo to restore them together rather than step-by-step.

The atomic action sequence must call StartAtomicAction first to begin recording the composite action and then EndAtomicAction to commit the unit to the history.

this.diagram1.Model.HistoryManager.StartAtomicAction("Custom Action");

// Perform the operations you want to group into a single undo entry here.
Syncfusion.Windows.Forms.Diagram.Rectangle rect =
    new Syncfusion.Windows.Forms.Diagram.Rectangle(100, 100, 100, 50);
this.diagram1.Model.AppendChild(rect);

this.diagram1.Model.HistoryManager.EndAtomicAction();
Me.diagram1.Model.HistoryManager.StartAtomicAction("Custom Action")

' Perform the operations you want to group into a single undo entry here.
Dim rect As New Syncfusion.Windows.Forms.Diagram.Rectangle(100, 100, 100, 50)
Me.diagram1.Model.AppendChild(rect)

Me.diagram1.Model.HistoryManager.EndAtomicAction()

Calling Undo after the sample above removes the appended rectangle in a single step labeled “Custom Action”.