Implementar operações de desfazer / refazer para adicionar / excluir itens de ListView
Eu tenho muitos problemas ao tentar implementar uma operação de desfazer / refazer em um controle ListView, apenas para adicionar / remover itens.
Eu percebi há muito tempo uma questão relativa aquiEstender esta classe para desfazer / refazer em um listview onde eu comecei várias recompensas de 50, 100, 200 e 300 pontos, um total de 650 pontos ... mas nenhum corpo poderia realmente me ajudar a finalizar este problema em semanas e meses.
Mas depois de um tempo nessa pergunta, finalmente, um usuário (@ThorstenC) me mostrou uma solução possível e uma ótima idéia, o código dele está incompleto, então o código dele é o que eu estou tentando realizar / terminar.
O problema é que o Simple "undo" funciona bem, mas quando eu tento refazer mais de 1 vez ele lança uma exceção sobre ele não pode adicionar o mesmo item novamente no listview, também o código tem mais problemas por exemplo no momento Não consigo refazer uma operação de desfazer ou desfazer uma operação de refazer.
Só preciso de ajuda para fazer um trabalho desfazer / refazer gerente para Listview Item adicionando / removendo, isso é tudo, eu escrevi a metade do código, preciso de ajuda para terminá-lo eu tenho uma bagunça na minha cabeça com isso.
Aqui está um projeto de fonte WinForms simples no VS2012 que eu enviei para testar o gerenciador de undo falha:
http://elektrostudios.tk/UndoManager.zip
Aqui está um vídeo para mostrar os erros que eu estou tentando desfazer / refazer:http://www.youtube.com/watch?v=MAzChURATpM
Aqui está a classe UndoManager do @ThorstenC com pequenos retoques:
Class ListView_UndoManager
Public Property Undostack As New Stack(Of ListView_Action)
Public Property Redostack As New Stack(Of ListView_Action)
Public Property IsDoingUndo As Boolean ' = False
Public Property IsDoingRedo As Boolean ' = False
Private action As ListView_Action = Nothing
''' <summary>
''' Undo the last action.
''' </summary>
''' <remarks></remarks>
Sub UndoLastAction()
If Undostack.Count = 0 Then Exit Sub ' Nothing to Undo.
action = Undostack.Pop ' Get the Action from Stack and remove it.
action.Operation.DynamicInvoke(action.data) ' Invoke the undo Action.
'Redostack = New Stack(Of ListView_Action)(Redostack)
'Redostack.Pop()
'Redostack = New Stack(Of ListView_Action)(Redostack)
End Sub
''' <summary>
''' Redo the last action.
''' </summary>
''' <remarks></remarks>
Sub RedoLastAction()
' If Redostack.Count = Undostack.Count Then Exit Sub
If Redostack.Count = 0 Then Exit Sub ' Nothing to Redo.
'Redostack = New Stack(Of ListView_Action)(Redostack) ' Reverse the Stack contents.
action = Redostack.Pop() ' Get the Action from Stack and remove it.
' action = Redostack.Peek()
action.Operation.DynamicInvoke(action.data) ' Invoke the redo Action.
'Redostack = New Stack(Of ListView_Action)(Redostack) ' Re-Reverse the Stack contents.
End Sub
End Class
Class ListView_Action
''' <summary>
''' Name the Undo / Redo Action
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Property name As String
''' <summary>
''' Points to a method to excecute
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Property Operation As [Delegate]
''' <summary>
''' Data Array for the method to excecute
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Property data As Object()
End Class
E aqui está o resto do código onde eu estou tentando desfazer / refazer Adicionando / Excluindo itens de lista:
Public Class Form1
Dim _undoManager As New ListView_UndoManager
Delegate Sub RemoveDelegate(item As ListViewItem)
Delegate Sub AddDelegate(item As ListViewItem)
Dim newItem As ListViewItem = Nothing
Sub AddItem(ByVal item As ListViewItem)
' // Crate an Undo Action
Dim u As New ListView_Action() With {.name = "Remove Item",
.Operation = New RemoveDelegate(AddressOf RemoveItem),
.data = New Object() {newItem}}
_undoManager.Undostack.Push(u)
ListView_Elektro1.AddItem(item)
End Sub
Sub RemoveItem(item As ListViewItem)
' // Create a Redo Action
Dim r As New ListView_Action() With {.name = "Add Item",
.Operation = New AddDelegate(AddressOf AddItem),
.data = New Object() {item}}
_undoManager.Redostack.Push(r)
' Remove the ListViewItem from ListView
ListView_Elektro1.RemoveItem(item)
End Sub
Private Sub Button_AddItem_Click(sender As Object, e As EventArgs) _
Handles Button_AddItem.Click
Dim index As String = CStr(ListView_Elektro1.Items.Count + 1)
newItem = New ListViewItem _
With {.Text = index}
newItem.SubItems.AddRange({"Hello " & index, "World " & index})
AddItem(newItem)
End Sub
Private Sub Button_RemoveItem_Click(sender As Object, e As EventArgs) _
Handles Button_RemoveItem.Click
newItem = ListView_Elektro1.Items.Cast(Of ListViewItem).Last
RemoveItem(newItem)
End Sub
Private Sub Button_Undo_Click(sender As Object, e As EventArgs) _
Handles Button_Undo.Click
' _undoManager.IsDoingUndo = True
_undoManager.UndoLastAction()
' _undoManager.IsDoingUndo = False
End Sub
Private Sub Button_Redo_Click(sender As Object, e As EventArgs) _
Handles Button_Redo.Click
'_undoManager.IsDoingRedo = True
_undoManager.RedoLastAction()
'_undoManager.IsDoingRedo = False
End Sub
Private Sub ListView_Elektro1_ItemAdded() _
Handles ListView_Elektro1.ItemAdded, _
ListView_Elektro1.ItemRemoved
Label_UndoCount_Value.Text = CStr(_undoManager.Undostack.Count)
Label_RedoCount_Value.Text = CStr(_undoManager.Redostack.Count)
End Sub
End Class