Using Undo in a Text Editor

Does anyone know a way to make the "Undo" command in a Text Editor work (the undo on the Project's tool bar). Now, I have tried saving a temp file and using a timer, etc..., but there must be a way to actually enable undo to do what it should (notice that in Microsoft's Wordpad, you can undo more than one change, so it's not just save a copy or make it a variable, etc... There has to be a way to make it be able to undo maybe 6 or 7 or all of the changes. That's what I need to know). Thanks for all the help you can give.
guinnessAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
hessConnect With a Mentor Commented:
if you make the object name match the real objects being used then there is no problem. txtMain.Text should be changed to the string that the undo will work on it can be any variable so if it was text1.text then change the cod to look like this:

 
Public Sub myundo(x As Integer)
       Static undotxtMain(32) As String
       If x = 1 Then
           For x = 31 To 0 Step -1
           undotxtMain(x + 1) = undotxtMain(x)
           Next x
           undotxtMain(0) = Text1.Text
       Else
           Text1.Text = undotxtMain(1)
           For x = 1 To 32
               undotxtMain(x - 1) = undotxtMain(x)
           Next x
           
           For x = 1 To 32
               undotxtMain(x - 1) = undotxtMain(x)
           Next x
           
       End If
       End Sub


the error 424 was created because the code refferd to an object that wasn't there so mak sure to make the 2 object references in the code point to real objects.
0
 
hessCommented:
this will give you 30+ level undo on a text box and will takes a max of 1meg mor memory the numbers in the for next loops and the elements in the arry could be increased to allow for greater undoabiltiy. In txtmain_change() this line must be added

Call myundo(1)

and in your undo menu item add

call myundo(2)

Public Sub myundo(x As Integer)
Static undotxtMain(32) As String
If x = 1 Then
    For x = 31 To 0 Step -1
    undotxtMain(x + 1) = undotxtMain(x)
    Next x
    undotxtMain(0) = txtMain.Text
Else
    txtMain.Text = undotxtMain(1)
    For x = 1 To 32
        undotxtMain(x - 1) = undotxtMain(x)
    Next x
   
    For x = 1 To 32
        undotxtMain(x - 1) = undotxtMain(x)
    Next x
   
End If
End Sub
0
 
rene100Commented:
hi guinnes

you must use the windows-api function 'SendMessage'
the first parameter is the handle of the texteditor, the
second EM_UNDO and both the following 0.
the only problem ist that you can't undo more than 1
change.

rene



0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
guinnessAuthor Commented:
I know you are going to cut me to shreads for doing this, but there is a problem in the code. I know, it may not be your fault, but it's gonna' kill me if I can't get this to work (I can create an Enterprise Solution but not a Text Editor). On the line after the "ELSE", I am getting Run-time Error '424', Object Expected and, since I didn't write the code, I can't figure out what it wants. Hess, if you hurry and re-answer I'll try and give you the points. Thanks.
0
 
anthonycCommented:
dude,

use the sendmessage solution, it's much nicer.

sendmessage text1.hwnd, EM_UNDO, 0&, 0& 'look in the API viewer for the decls of sendmessage and em_undo
0
 
kamallCommented:
guinness,
Certainly, your problem is that your text box has a name other than 'txtMain'.
0
 
kamallCommented:
anthonyc,
I wanted to answer the question before you, but I found it unfair to do that since hess's answer was a good one.

guinness,
If my previous comment solves your problem, please think of to whom you should award the points.

Regards to all.
0
 
guinnessAuthor Commented:
I would like for Hess's answer to work, and, my Text Box name IS txtMain. I've already checked that.
0
 
anthonycCommented:
hah.. you'd rather go with the non-windows solution.... fine.
0
 
guinnessAuthor Commented:
Hess: It looks as though the solution that was given earlier did NOT work. What it DID do was create a New Document named frmDocument (It opened frmDocument basically). Wanna try and fix it or should I go for another solution/
0
 
hessCommented:
if your text box name is txtmain.text then i don't know what the problem is. I works on my computer.
0
 
HATCHETCommented:
Quick comment... if you use the SendMessage API, you will NOT be able to undo any Cut/Copy/Paste/Delete functionality that you add to your project manually using say the .SelText or .Text properties.  For some reason, it doesn't acknowledge such "manual" edits as valid edits to undo.  If you type it, the SendMessage API will undo it... that's about the extent of it.  NO MULTI-LEVEL UNDO... that's why this code is more advantageous.

Windows isn't always righ!    ; P

HATCHET
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.