Collection für Undo/Redo

    • VB.NET

      Collection für Undo/Redo

      Dieser Code ist gut geeignet als Library-Code, da man ja in so gut wie jedem Programm eine Rückwärts-Taste hat:

      VB.NET-Quellcode

      1. Imports Microsoft.VisualBasic
      2. Public Class UndoCollection
      3. Public UCollection As New Collection
      4. Dim LastInsert As Integer = 0
      5. Dim content As String = ""
      6. 'Dim a As Integer = 0
      7. Public Sub Add(ByVal link As String)
      8. Dim result As Integer = UCollection.Count
      9. If result < 5 Then
      10. content = link
      11. UCollection.Add(link)
      12. LastInsert += 1
      13. 'a += 1
      14. End If
      15. End Sub
      16. Public Sub Remove()
      17. Dim result As Integer = UCollection.Count
      18. If 0 < result AndAlso result <= 5 Then
      19. UCollection.Remove(LastInsert)
      20. LastInsert -= 1
      21. 'a -= 1
      22. End If
      23. End Sub
      24. Public Function Undo() As String
      25. Dim result As Integer = UCollection.Count
      26. Dim value As String = ""
      27. Dim item As Object
      28. If result > 0 Then
      29. item = UCollection.Item(LastInsert)
      30. value = item.ToString
      31. Remove()
      32. End If
      33. Return value
      34. End Function
      35. End Class
      36. Public Class RedoCollection
      37. Public RCollection As New Collection
      38. Dim LastInsert As Integer = 0
      39. 'Dim r As Integer = 0
      40. Public Sub Add(ByVal link As String)
      41. Dim result As Integer = RCollection.Count
      42. If result < 5 Then
      43. RCollection.Add(link)
      44. LastInsert += 1
      45. 'a += 1
      46. End If
      47. End Sub
      48. Public Sub Remove()
      49. Dim result As Integer = RCollection.Count
      50. If 0 < result AndAlso result <= 5 Then
      51. RCollection.Remove(LastInsert)
      52. LastInsert -= 1
      53. 'a -= 1
      54. End If
      55. End Sub
      56. Public Function Redo() As String
      57. Dim result As Integer = RCollection.Count
      58. Dim value As String = ""
      59. Dim item As Object
      60. If result > 0 Then
      61. item = RCollection.Item(LastInsert)
      62. value = item.ToString
      63. Remove()
      64. End If
      65. Return value
      66. End Function
      67. End Class


      Aus der Formklasse (meistens) spricht man ihn so an, wenn man ihn als dll-importiert:

      VB.NET-Quellcode

      1. UndoRedoLibrary 'Namespace
      2. UndoRedoLibrary.UndoCollection 'Klasse UndoCollection
      3. UndoRedoLibrary.UndoCollection.Add(link) 'Die Sub Add (der Klasse UndoCollection) die als Wert, den zu speichernden Inhalt als String benötigt.
      4. UndoRedoLibrary.UndoCollection.Undo() 'Die Funktion Undo(), die den String des zuletzt hinzugefügten Objekts zurückgibt.
      5. UndoRedoLibrary.RedoCollection 'Klasse RedoCollection
      6. UndoRedoLibrary.RedoCollection.Add(link) 'Die Sub Add (der Klasse RedoCollection), die als Wert, den zu speichernden Inhalt als String benötigt.
      7. UndoRedoLibrary.RedoCollection.Redo() ' Die Funktion Redo(), die den String des zuletzt hinzugefügten Objekts zurückgibt.
      8. UndoRedoLibrary.UndoCollection.UCollection 'Das Collection-Objekt der Klasse UndoCollection.
      9. UndoRedoLibrary.RedoCollection.RCollection 'Das Collection-Objekt der Klasse RedoCollection.
      10. UndoRedoLibrary.UndoCollection.UCollection.Count 'Gibt die Anzahl der gespeicherten Inhalte als Integer zurück.
      11. UndoRedoLibrary.RedoCollection.RCollection.Count 'Gibt die Anzahl der gespeicherten Inhalte als Integer zurück.


      Um eine unendliche Speicherung hinzubekommen, einfach bei den beiden Add-Subs die Beschränkung, mit 5 als maximalen Inhalt entnehmen.

      Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „pluginbauer“ ()