Results 1 to 5 of 5

Thread: A reliable Multiple Undo/Redo

  1. #1

    Thread Starter
    Member docHoliday's Avatar
    Join Date
    Sep 1999
    Location
    Stamford, CT, USA
    Posts
    48

    A reliable Multiple Undo/Redo

    I found some code on the web for multiple undo/redo, but it only holds up to 1000x undo and then throws and out of boundaries error when undo 1001 tries to join the array. I tried writing a function that recycled an undo array when the max limit was reached, but it never replaced the contents of the array back into the text box correctly.

    Anyone have or know where to find a reliable multiple undo/redo code snippet to use?

    P.S. I've used the CAN_UNDO and UNDO API functions and they're just not versatile enough. I'm looking for at least 10x undo/redo.
    "A balm? That's a dangerous animal. Throw it in the trough!"
    - Monty Python

  2. #2
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530

    Here is an example to get some ideas

    Add a new class called clschanges then add this code:

    Public ControlName As String
    Public ControlValue As Variant


    then add this to a form:
    VB Code:
    1. Private Const MAXCHANGES As Integer = 10 'change this value to increase/decrease buffer
    2.  
    3. Private Sub ClearChanges()
    4. If colChanges.Count = 0 Then Exit Sub
    5. Dim i%
    6. For i = colChanges.Count To 1 Step -1
    7.     colChanges.Remove i
    8. Next i
    9. End Sub
    10.  
    11.  
    12. Private Sub AddChange(strControlName As String, varValue As Variant)
    13.  
    14. Dim objNewChange As New clsChanges
    15. Dim objLastChange As New clsChanges
    16. Dim i%
    17.  
    18. If colChanges.Count > 1 Then
    19.     Set objLastChange = colChanges(colChanges.Count)
    20.    
    21.     If objLastChange.ControlName = strControlName And _
    22.         objLastChange.ControlValue = varValue Then  ' check if new change = last change added
    23.         Exit Sub
    24.     Else
    25.         objNewChange.ControlName = strControlName
    26.         objNewChange.ControlValue = varValue
    27.         colChanges.Add objNewChange
    28.     End If
    29. Else 'add initial entry to change collection
    30.     objNewChange.ControlName = strControlName
    31.     objNewChange.ControlValue = varValue
    32.     colChanges.Add objNewChange
    33. End If
    34.  
    35.    
    36. If colChanges.Count > MAXCHANGES Then colChanges.Remove 1 ' if added one more than max allowed delete oldest entry
    37.  
    38. lngChangeBookmark = colChanges.Count ' resest bookmark to newest entry
    39.  
    40. Set objLastChange = Nothing
    41. Set objNewChange = Nothing
    42. End Sub
    43.  
    44.  
    45. Private Sub Undo()
    46. If colChanges.Count < 1 Or lngChangeBookmark < 2 Then Exit Sub ' no undo possible
    47. Dim ctr As Control
    48.    
    49.     For Each ctr In Me.Controls
    50.         If LCase(ctr.Name) = LCase(colChanges(lngChangeBookmark).ControlName) Then
    51.             Select Case LCase(TypeName(ctr))
    52.                 Case "combobox"
    53.                     While ctr.Name = colChanges(lngChangeBookmark).ControlName And _
    54.                             ctr.ListIndex = colChanges(lngChangeBookmark).ControlValue And _
    55.                             lngChangeBookmark > 1
    56.                         lngChangeBookmark = lngChangeBookmark - 1
    57.                     Wend
    58.                     ctr.ListIndex = colChanges(lngChangeBookmark).ControlValue
    59.                     Exit For
    60.                
    61.                 Case "maskedbox", "textbox"
    62.                     While ctr.Name = colChanges(lngChangeBookmark).ControlName And _
    63.                             ctr.Text = colChanges(lngChangeBookmark).ControlValue And _
    64.                             lngChangeBookmark > 1
    65.                         lngChangeBookmark = lngChangeBookmark - 1
    66.                     Wend
    67.                     ctr.Text = colChanges(lngChangeBookmark).ControlValue
    68.                     Exit For
    69.                
    70.                 Case Else
    71.                     MsgBox "Sub Undo() does not currently include the following control:" & LCase(TypeName(ctr)) & vbCrLf & "Please ammend."
    72.             End Select
    73.             End If
    74.     Next ctr
    75.     lngChangeBookmark = lngChangeBookmark - 1 ' reposition bookmark
    76. End Sub
    77.  
    78. Private Sub Redo()
    79. Dim ctr As Control
    80.     If colChanges.Count = 0 Then Exit Sub ' no redo possible
    81.     If lngChangeBookmark = colChanges.Count Then Exit Sub ' no redo possible
    82.    
    83.     lngChangeBookmark = lngChangeBookmark + 1 ' reposition bookmark
    84.        
    85.     For Each ctr In Me.Controls
    86.         If LCase(ctr.Name) = LCase(colChanges(lngChangeBookmark).ControlName) Then
    87.             Select Case LCase(TypeName(ctr))
    88.                 Case "combobox"
    89.                     While ctr.Name = colChanges(lngChangeBookmark).ControlName _
    90.                             And ctr.ListIndex = colChanges(lngChangeBookmark).ControlValue And _
    91.                             lngChangeBookmark < MAXCHANGES
    92.                         lngChangeBookmark = lngChangeBookmark + 1
    93.                     Wend
    94.                     ctr.ListIndex = colChanges(lngChangeBookmark).ControlValue
    95.                     Exit For
    96.                    
    97.                 Case "maskedbox", "textbox"
    98.                     While ctr.Name = colChanges(lngChangeBookmark).ControlName And _
    99.                             ctr.Text = colChanges(lngChangeBookmark).ControlValue And _
    100.                             lngChangeBookmark < MAXCHANGES
    101.                         lngChangeBookmark = lngChangeBookmark + 1
    102.                     Wend
    103.                     ctr.Text = colChanges(lngChangeBookmark).ControlValue
    104.                     Exit For
    105.                    
    106.                 Case Else
    107.                     MsgBox "Sub Undo() does not currently include the following control:" & _
    108.                             LCase(TypeName(ctr)) & vbCrLf & "Please ammend."
    109.             End Select
    110.         End If
    111.     Next ctr
    112. End Sub

    Then in the got lost focus events for the controls you want to enable undo redo:
    VB Code:
    1. Private Sub txtTechBusPhone_GotFocus()
    2.     varControlValue = Me.txtTechBusPhone.Text
    3. End Sub
    4.  
    5. Private Sub txtTechBusPhone_LostFocus()
    6.     If Me.txtTechBusPhone.Text <> varControlValue Then
    7.         Call AddChange("txtTechBusPhone", varControlValue)
    8.         Call AddChange("txtTechBusPhone", Me.txtTechBusPhone.Text)
    9.     End If
    10. End Sub


    In Undo button:
    Call Undo

    In redo Button:
    Call Redo


    This is an old example that works with comboboxes, textboxes and masked textboxes; it should give you some good ideas.

  3. #3

    Thread Starter
    Member docHoliday's Avatar
    Join Date
    Sep 1999
    Location
    Stamford, CT, USA
    Posts
    48
    Is colChanges a list box control or array or collection? I didn't see it declared in the example.
    "A balm? That's a dangerous animal. Throw it in the trough!"
    - Monty Python

  4. #4
    DaoK
    Guest
    I named a textbox : txtTechBusPhone

    and
    I have an error therer ? :

    If colChanges.Count > 1 Then

  5. #5
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530

    Attached is an example project

    I have created an example project to demonstrate the code I posted above which was missing a couple of declarations.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width