You are on page 1of 2

01 Public Class UndoRedoClass(Of T)

02 Private UndoStack As Stack(Of T)


03 Private RedoStack As Stack(Of T)
04
05 Public CurrentItem As T
06 Public Event UndoHappened As EventHandler(Of UndoRedoEventArgs)
07 Public Event RedoHappened As EventHandler(Of UndoRedoEventArgs)
08
09 Public Sub New()
10 UndoStack = New Stack(Of T)
11 RedoStack = New Stack(Of T)
12 End Sub
13
14 Public Sub Clear()
15 UndoStack.Clear()
16 RedoStack.Clear()
17 CurrentItem = Nothing
18 End Sub
19
20 Public Sub AddItem(ByVal item As T)
21 If CurrentItem IsNot Nothing Then UndoStack.Push(CurrentItem)
22 CurrentItem = item
23 RedoStack.Clear()
24 End Sub
25
26 Public Sub Undo()
27 RedoStack.Push(CurrentItem)
28 CurrentItem = UndoStack.Pop()
29 RaiseEvent UndoHappened(Me, New UndoRedoEventArgs(CurrentItem))
30 End Sub
31
32 Public Sub Redo()
33 UndoStack.Push(CurrentItem)
34 CurrentItem = RedoStack.Pop
35 RaiseEvent RedoHappened(Me, New UndoRedoEventArgs(CurrentItem))
36 End Sub
37
38 Public Function CanUndo() As Boolean
39 Return UndoStack.Count > 0
40 End Function
41
42 Public Function CanRedo() As Boolean
43 Return RedoStack.Count > 0
44 End Function
45
46 Public Function UndoItems() As List(Of T)
47 Return UndoStack.ToList
48 End Function
49
50 Public Function RedoItems() As List(Of T)
51 Return RedoStack.ToList
52 End Function
53 End Class
54
55 Public Class UndoRedoEventArgs
56 Inherits EventArgs
57
58 Private _CurrentItem As Object
59 Public ReadOnly Property CurrentItem() As Object
60 Get
61 Return _CurrentItem
62 End Get
63 End Property
64
65 Public Sub New(ByVal currentItem As Object)
66 _CurrentItem = currentItem
67 End Sub
68 End Class

Below is the explanation of various public elements of this class:

CurrentItem: This holds the current item. It is neither in the undo stack nor the redo stack. It is in
between the two of them.
UndoHappened: This event is fired when Undo method is called.
RedoHappened: This event is fired when Redo method is called.
Clear: Calling this method will clear both the stacks as well as the current item. It is as good as
creating a new instance of this class.
AddItem: This method is used to add items to our undo list. This is the only way at present to add
items to our undo stack.
Undo: Calling this method will undo the last item added to our undo list. The item is moved to
redo list.
Redo: Calling this method will redo the last item added to the redo list. The item is moved to
undo list.
CanUndo: This method can be used to check whether undo is possible or not. It is useful when we
want to take decisions like disabling the undo bu ons etc. when undo is not possible.
CanRedo: This method can be used to check whether redo is possible or not. It is useful when we
want to take decisions like disabling the redo bu ons etc. when redo is not possible.
UndoItems: Returns a list of undo items we currently have in the undo stack.
RedoItems: Returns a list of redo items we currently have in the redo stack.
UndoRedoEventArgs: This is the eventargs for our UndoHappened and RedoHappened events.
The e.CurrentItem will have the current item which was undone/redone.

You might also like