Solved

Using Undo in a Text Editor

Posted on 1998-08-13
12
265 Views
Last Modified: 2010-04-30
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.
0
Comment
Question by:guinness
  • 3
  • 3
  • 2
  • +3
12 Comments
 

Expert Comment

by:hess
Comment Utility
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
 
LVL 2

Expert Comment

by:rene100
Comment Utility
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
 

Author Comment

by:guinness
Comment Utility
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
 
LVL 6

Expert Comment

by:anthonyc
Comment Utility
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
 
LVL 7

Expert Comment

by:kamall
Comment Utility
guinness,
Certainly, your problem is that your text box has a name other than 'txtMain'.
0
 
LVL 7

Expert Comment

by:kamall
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:guinness
Comment Utility
I would like for Hess's answer to work, and, my Text Box name IS txtMain. I've already checked that.
0
 
LVL 6

Expert Comment

by:anthonyc
Comment Utility
hah.. you'd rather go with the non-windows solution.... fine.
0
 

Author Comment

by:guinness
Comment Utility
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
 

Accepted Solution

by:
hess earned 150 total points
Comment Utility
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
 

Expert Comment

by:hess
Comment Utility
if your text box name is txtmain.text then i don't know what the problem is. I works on my computer.
0
 
LVL 3

Expert Comment

by:HATCHET
Comment Utility
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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now