Library code snippets

Undo and Redo

Undo and Redo

Use AddUndo to start adding pauses in-between when the user is typing. One way to do this is to use this code.

Private Sub Text1_Change()
   Timer1.Interval = 5000
End Sub

Private Sub Timer1_Timer()
   Undo1.AddUndo Text1.Text
   Timer1.Interval = 0
End Sub

When you want to undo use Undo and Redo to redo.

Dim OldContent As String
Dim Last As UndoDirection
Public Enum UndoDirection
   DirUndo = 1
   DirRedo = 2
End Enum

Public Function Undo()
Dim Front As Double
Dim Back As Double
Dim StrFront As String
Dim StrBack As String
Dim Changed As String

   If Last = DirRedo Or Last = 0 Then
        Last = DirUndo
       Undo
   End If
   Last = DirUndo
   
   If lstUndo.ListCount = 0 Then
       Undo = OldContent
       Exit Function
   End If
   
   lstUndo.Selected(lstUndo.ListCount - 1) = True
   
   Undo = lstUndo.Text
   
   OldContent = Undo
   
   lstRedo.AddItem lstUndo.Text

   lstUndo.RemoveItem lstUndo.ListCount - 1
End Function
Public Function Redo()

   If Last = DirUndo Or Last = 0 Then
       Last = DirRedo
       Redo
   End If
   Last = DirRedo
   
   If lstRedo.ListCount = 0 Then
       Redo = OldContent
       Exit Function
   End If
   
   lstRedo.Selected(lstRedo.ListCount - 1) = True

   Redo = lstRedo.Text
   
   OldContent = Redo
   
   lstUndo.AddItem lstRedo.Text
   lstRedo.RemoveItem lstRedo.ListCount - 1
End Function

Public Sub AddUndo(NewContent As String)
Dim Content As String
Dim XStr As String
   Content = NewContent
   
   If OldContent = Content Then Exit Sub
   
   lstRedo.Clear
       
   lstUndo.AddItem Content
   OldContent = NewContent
End Sub

Comments

  1. 19 Dec 2003 at 12:54

    First, could you please explain at the beginning of code samples exactly what needs to be added to the form before your code can be used (i.e the lstUndo and lstRedo list boxes) and I'm sorry but this code simply is not an undo/redo code.  It simply deletes all the text in the box on an undo, then puts it bak in agen on a redo, regardless of what changed

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Undo and Redo.

Leave a comment

Sign in or Join us (it's free).

Nick Avery I am as a web developer for a small company, working for a small company. I work on banking websites and verious related projects.
AddThis

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback