Solved

Using Undo in a Text Editor

Posted on 1998-08-13
12
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +3
12 Comments
 

Expert Comment

by:hess
ID: 1468745
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
ID: 1468746
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
ID: 1468747
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 6

Expert Comment

by:anthonyc
ID: 1468748
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
ID: 1468749
guinness,
Certainly, your problem is that your text box has a name other than 'txtMain'.
0
 
LVL 7

Expert Comment

by:kamall
ID: 1468750
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
 

Author Comment

by:guinness
ID: 1468751
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
ID: 1468752
hah.. you'd rather go with the non-windows solution.... fine.
0
 

Author Comment

by:guinness
ID: 1468753
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
ID: 1468754
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
ID: 1468755
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
ID: 2181736
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

739 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