Results 1 to 20 of 20

Thread: Unlimited Undo/Redo

  1. #1

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755

    Talking Unlimited Undo/Redo

    I need an unlimited undo/redo code for my app...
    i've found a code for RichTextBoxes but it didnt support
    pictureboxes so i relly need som help with this!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  2. #2
    Lively Member Peder's Avatar
    Join Date
    Apr 2001
    Posts
    102
    This should work...
    VB Code:
    1. ' #VBIDEUtils#************************************************************
    2. ' * Programmer Name  : Steve McMahon
    3. ' * Web Site         : [url]http://www.vbaccelerator.com/[/url]
    4. ' * E-Mail           : [email][email protected][/email]
    5. ' * Date             : 20/12/1999
    6. ' * Time             : 10:17
    7. ' **********************************************************************
    8. ' * Comments         : Adding Multiple Undo & Redo
    9. ' *
    10. ' * The RichTextBox actually supports multiple undo and redo. However,
    11. ' * this functionality is hidden from VB programmers.
    12. ' * In order to be able to use the undo and redo
    13. ' * facilities, you need to add the following code.
    14. ' *
    15. ' **********************************************************************
    16.  
    17. ' Add this code to the Form_Load() event of the form that contains the RichTextBox control. We are calling the RichTextBox rtfText
    18.  
    19. Dim lStyle As Long
    20. '// required to 'reveal' multiple undo
    21. '// set rich text box style
    22. lStyle = TM_RICHTEXT Or TM_MULTILEVELUNDO Or TM_MULTICODEPAGE
    23. SendMessageLong rtfText.hwnd, EM_SETTEXTMODE, lStyle, 0
    24.  
    25. ' Then, add the code below. This code also adds cut/copy/paste/clear functionality, and expects the following menu items:
    26.  
    27. Menu Name Caption
    28. mnuEdit &Edit
    29. mnuEditUndo &Undo
    30. mnuEditRedo &Redo
    31. mnuEditCut Cu&t
    32. mnuEditCopy &Copy
    33. mnuEditPaste &Paste
    34. mnuEditClear C&lear
    35.  
    36. Call the UpdateItems procedure in the mnuEdit_Click() event. This procedure updates the menu items.
    37.  
    38. Public Property Get UndoType() As ERECUndoTypeConstants
    39.    UndoType = SendMessageLong(rtfText.hWnd, EM_GETUNDONAME, 0, 0)
    40. End Property
    41. Public Property Get RedoType() As ERECUndoTypeConstants
    42.    RedoType = SendMessageLong(rtfText.hWnd, EM_GETREDONAME, 0, 0)
    43. End Property
    44. Public Property Get CanPaste() As Boolean
    45.    CanPaste = SendMessageLong(rtfText.hWnd, EM_CANPASTE, 0, 0)
    46. End Property
    47. Public Property Get CanCopy() As Boolean
    48.    If rtfText.SelLength < 0 Then
    49.       CanCopy = True
    50.    End If
    51. End Property
    52. Public Property Get CanUndo() As Boolean
    53.    CanUndo = SendMessageLong(rtfText.hWnd, EM_CANUNDO, 0, 0)
    54. End Property
    55. Public Property Get CanRedo() As Boolean
    56.    CanRedo = SendMessageLong(rtfText.hWnd, EM_CANREDO, 0, 0)
    57. End Property
    58.  
    59. '///////////////////////////////////////////////////////
    60. '// Methods
    61. Public Sub Undo()
    62.    SendMessageLong rtfText.hWnd, EM_UNDO, 0, 0
    63. End Sub
    64. Public Sub Redo()
    65.    SendMessageLong rtfText.hWnd, EM_REDO, 0, 0
    66. End Sub
    67. Public Sub Cut()
    68.    SendMessageLong rtfText.hWnd, WM_CUT, 0, 0
    69. End Sub
    70. Public Sub Copy()
    71.    SendMessageLong rtfText.hWnd, WM_COPY, 0, 0
    72. End Sub
    73. Public Sub Paste()
    74.    SendMessageLong rtfText.hWnd, WM_PASTE, 0, 0
    75. End Sub
    76. Public Sub Clear()
    77.    rtfText.SelText = Empty
    78. End Sub
    79. Public Sub UpdateItems()
    80.    Dim bCanUndo As Boolean
    81.    '// Undo/Redo options:
    82.    bCanUndo = CanUndo
    83.    mnuEditUndo.Enabled = bCanUndo
    84.    '// Set Undo Text
    85.    If (bCanUndo) Then
    86.       mnuEditUndo.Caption = "&Undo " & TranslateUndoType(UndoType)
    87.    Else
    88.       mnuEditUndo.Caption = "&Undo"
    89.    End If
    90.    '// Set Redo Text
    91.    bCanUndo = CanRedo
    92.    If (bCanUndo) Then
    93.       mnuEditRedo.Caption = "&Redo " & TranslateUndoType(RedoType)
    94.    Else
    95.       mnuEditRedo.Caption = "&Redo"
    96.    End If
    97.    mnuEditRedo.Enabled = bCanUndo
    98.    tbToolBar.Buttons("Redo").Enabled = bCanUndo
    99.    '// Cut/Copy/Paste/Clear options
    100.    mnuEditCut.Enabled = CanCopy
    101.    mnuEditCopy.Enabled = CanCopy
    102.    mnuEditPaste.Enabled = CanPaste
    103.    mnuEditClear.Enabled = CanCopy
    104. End Sub
    105. '// Returns the undo/redo type
    106. Private Function TranslateUndoType(ByVal eType As ERECUndoTypeConstants) As String
    107.    Select Case eType
    108.       Case ercUID_UNKNOWN
    109.          TranslateUndoType = "Last Action"
    110.       Case ercUID_TYPING
    111.          TranslateUndoType = "Typing"
    112.       Case ercUID_PASTE
    113.          TranslateUndoType = "Paste"
    114.       Case ercUID_DRAGDROP
    115.          TranslateUndoType = "Drag Drop"
    116.       Case ercUID_DELETE
    117.          TranslateUndoType = "Delete"
    118.       Case ercUID_CUT
    119.          TranslateUndoType = "Cut"
    120.    End Select
    121. End Function
    122.  
    123. ' Then, add this code to a module
    124.  
    125. '// View Types
    126. Public Enum ERECViewModes
    127.    ercDefault = 0
    128.    ercWordWrap = 1
    129.    ercWYSIWYG = 2
    130. End Enum
    131. '// Undo Types
    132. Public Enum ERECUndoTypeConstants
    133.    ercUID_UNKNOWN = 0
    134.    ercUID_TYPING = 1
    135.    ercUID_DELETE = 2
    136.    ercUID_DRAGDROP = 3
    137.    ercUID_CUT = 4
    138.    ercUID_PASTE = 5
    139. End Enum
    140. '// Text Modes
    141. Public Enum TextMode
    142.    TM_PLAINTEXT = 1
    143.    TM_RICHTEXT = 2 ' /* default behavior */
    144.    TM_SINGLELEVELUNDO = 4
    145.    TM_MULTILEVELUNDO = 8 ' /* default behavior */
    146.    TM_SINGLECODEPAGE = 16
    147.    TM_MULTICODEPAGE = 32 ' /* default behavior */
    148. End Enum
    149.  
    150. Public Const EM_SETTEXTMODE = (WM_USER + 89)
    151. Public Const EM_UNDO = &HC7
    152. Public Const EM_REDO = (WM_USER + 84)
    153. Public Const EM_CANPASTE = (WM_USER + 50)
    154. Public Const EM_CANUNDO = &HC6&
    155. Public Const EM_CANREDO = (WM_USER + 85)
    156. Public Const EM_GETUNDONAME = (WM_USER + 86)
    157. Public Const EM_GETREDONAME = (WM_USER + 87)
    158.  
    159. Public Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    160. Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
    Last edited by Hack; Dec 6th, 2005 at 12:22 PM.

  3. #3

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    thanks ill try that...
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  4. #4
    Addicted Member
    Join Date
    Mar 2003
    Posts
    135

    Unhappy when i try to use

    When i try to use this code i get a
    Constant Expression Required Error Message with
    WM_USER

    can anyone help?

    thanks

  5. #5
    Frenzied Member ice_531's Avatar
    Join Date
    Aug 2002
    Location
    Sitting w/ Bob Status: -Next -To- Null- Friend: Philip
    Posts
    1,152
    im guessing it isnt declared in that code

    Public Const WM_USER = &H400
    :::`DISCLAIMER`:::
    Do NOT take anything i have posted to be truthful in any way, shape or form.
    Thank You!

    --------------------------------
    "Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
    "Finaly I can look as gay as I want..." - NoteMe
    Languages: VB6, BASIC, Java, C#. C++

  6. #6

  7. #7
    Addicted Member
    Join Date
    Mar 2003
    Posts
    135
    hey martin buddy the code works now that that is declared.

    and thanks alot ice i apreciate your help

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by brainprogrammer
    hey martin buddy the code works now that that is declared.

    and thanks alot ice i apreciate your help
    With VB6? If so I'd really appreciate it if you could attach a small demo app.

  9. #9
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    Re: Unlimited Undo/Redo

    * bump *

    with this code there are several things that are not defined.. you have to google the constants to get the values

    anyway, i setup a demo project, and the identification of the kind of undo works ie Undo Typing, BUT it still behaves the same. that is, it acts just as if there was only 1 level of undo. pressing undo just toggles back and forth between the last action being there or not.

    if someone could get this working that would be great

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Unlimited Undo/Redo

    I found this, but haven't tried it, yet.

  11. #11

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Unlimited Undo/Redo

    Oops. I hunted for quite a while, too. No idea. I'll see if i can find it in my history. It says this guy started the site. Still haven't tried it, but over 10K persons downloaded it.


    http://www.developerfusion.co.uk/show/358/

    I will try it, though. It does work with images, as well a text. A worthy addition to my code library.
    Last edited by dglienna; Feb 23rd, 2005 at 06:23 PM.

  13. #13
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Unlimited Undo/Redo

    I have attached the demo I was using, just so people don't keep having to create it from the code above.

    The reason why this code doesn't work, and only seems to give you a single level undo, is because it's text mode is set to that.
    In form load the code is:
    Code:
    Private Sub Form_Load()
    Dim lStyle As Long
        lStyle = TM_RICHTEXT Or TM_MULTILEVELUNDO Or TM_MULTICODEPAGE
        SendMessageLong rtfText.hwnd, EM_SETTEXTMODE, lStyle, 0
    End Sub
    This should set the mode to multi level undo.
    But what actually happens is that it doesn't get set.
    I changed the above code to:
    Code:
    Private Sub Form_Load()
    Dim lStyle As Long
        lStyle = TM_RICHTEXT Or TM_MULTILEVELUNDO Or TM_MULTICODEPAGE
        MsgBox "Style to be set: " & lStyle
        SendMessageLong rtfText.hwnd, EM_SETTEXTMODE, lStyle, 0
        MsgBox "Style after change: " & SendMessage(rtfText.hwnd, EM_GETTEXTMODE, 0&, 0&)
    End Sub
    and added the following to the module:
    Code:
    public Const EM_GETTEXTMODE = (WM_USER + 90)
    What we actually see is that lStyle = 42, which is what we want. However, when we read back the style it's only got a value of 38
    This means it's style is:
    Code:
    lStyle = TM_RICHTEXT Or TM_SINGLELEVELUNDO Or TM_MULTICODEPAGE
    So, I can think of only 2 reasons why this doesn't work. The SendMessageLong command is wrong (I have tried to use SendMessage but that fails too), or it doesn't actually support this...

    Maybe we can solve this now we know whats up.

    Woof
    Attached Files Attached Files

  14. #14
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Unlimited Undo/Redo

    Mine doesn't have any advanced features, but it does do undo and redo for unlimited numbers, which can be set automatically. Default is 100.

  15. #15
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    Re: Unlimited Undo/Redo

    the only problem with keeping track yourself is that the RTB is a little smarter about it.

    when the RTB is in control if you type 'animal' and undo, 'animal' is undone.

    but in the other example, l is undone, then a, then m, then i, then n, then a

  16. #16
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Unlimited Undo/Redo

    Quote Originally Posted by dglienna
    Mine doesn't have any advanced features, but it does do undo and redo for unlimited numbers, which can be set automatically. Default is 100.
    And...eeeerrr...are you going to post the code?

    Woof

  17. #17
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Unlimited Undo/Redo

    I didn't write it, just found it, tested, and posted the link to it (above)
    I agree that it could be improved, but in actuality, you are deleting one letter at at time, so it's right. I didn't paste but it un-did pasting, and then deleting a picture.

  18. #18
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Unlimited Undo/Redo

    Sorry to bump an old thread, but I am having trouble with this exact problem at the moment.

    Does anyone have any idea how to solve the problem that Woka mentioned in post #13, because that is the same problem I encountered yesterday.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  19. #19
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Unlimited Undo/Redo

    The EM_SETTEXTMODE message is only supported in RichEdit 2.0 and above, however the VB RichTextBox is based on RichEdit 1.0. VBAccelerator used to have a RichTextBox control that was created purely from API which supported this. It is still available but only as part of some of the downloadable tools.

  20. #20
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Unlimited Undo/Redo

    That makes sense. I ran across the RichEdit control from vbAccelerator, but whenever I try to run it I get different errors. I think I have all the type libraries and such registered that I need to. I will start a new thread about it when I get home.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

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