I\'m using a thir party code to manage undo/redo operations in a WindowsForm project.
I need to extend the Class to manage undo/redo operations in a Listview, this m
Try this approach: Forget this current implementation, start implementing your own Undo/Redo class.
Every Method that manipulates something needs to create it own Undo methods. Store the delegates and invoke it when required. I have make an example with a simple Add / Remove of listview items.
Public Class Form1
Dim _undoManager As New UndoManager
'''
''' Delegates to Remove an item
'''
'''
'''
Delegate Sub RemoveDelegate(item As Object)
'''
''' Delegates to Add an Item
'''
'''
'''
Delegate Sub AddDelegate(text As String)
Sub AddItem(name As String)
Dim newItem = ListView1.Items.Add(name)
'Crate an Undo Operation
Dim a As New action() With {.name = "Remove Item",
.Operation = New RemoveDelegate(AddressOf RemoveItem),
.data = New Object() {newItem}}
_undoManager.Undostack.Push(a)
' Create a Redo
Dim a As New action() With {.name = "Add Item",
.Operation = New AddDelegate(AddressOf AddItem),
.data = New Object() {name}}
_undoManager.Redostack.Push(a)
End Sub
Sub RemoveItem(item As Object)
ListView1.Items.Remove(item)
End Sub
'''
''' Changes the Text of the Item
'''
'''
'''
'''
Sub changetext(item As Object, text As String)
Dim oldtext As String = item.text
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.AddItem("new Item")
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
_undoManager.UndoLastAction()
End Sub
End Class
Class UndoManager
Public Property Undostack As New Stack(Of action)
Public Property Redostack As New Stack(Of action)
'''
''' Undos the top of the stack
'''
'''
Sub UndoLastAction()
Dim action As action = Undostack.Pop ' Get the Action from Stack
action.Operation.DynamicInvoke(action.data) ' Invoke the reverse Action
End Sub
Sub RedoLastAction()
Dim action As action = Redostack.Peek' Get the Action from Stack, but dont remove
action.Operation.DynamicInvoke(action.data) ' Invoke the reverse Action
End Sub
End Class
Class action
'''
''' Name the Undo / Redo Action
'''
'''
'''
'''
Property name As String
'''
''' Points to a method to excecute
'''
'''
'''
'''
Property Operation As [Delegate]
'''
''' Data Array for the method to excecute
'''
'''
'''
'''
Property data As Object()
End Class