How do I get an 'Undo' command to work?



Read and add posts to the Visual Basic 6.0 Forums

Download some of my programs and OCXs

Read the Tips here

The old style forum is still available here




From: Steve Fekete
Date: Tuesday 27 March 2001 3:38 AM
Subject: vb help
Question: Hello,
      My name is Steve. I'm currently putting together another notepad that
will save files w/ a Java extension rather than the .txt extension. I'm stuck
on two points. The first is an undo command. I cant figure out how to program
an undo command.
      The second I'm not sure if its even possible. I would to be able to
give out this program when I'm done. On the menu bar I have created three
functions. One is for the date, so all you have to do is hit the date and the
program automatically puts the date in for you. The second is for your name,
so all you have to hit it and your name appears. Now with the third one, it
calls a form (frmInput.Show)so you enter your name and it stores the string
in a variable. It works but I'm wondering if its possible to create a kind of
"permanent" variable that will store someone's name, so you only have to
enter your name once. I would think this would be possible (like a password
being stored), but I this is beyond my realm of knowledge.
      I would appreaciate any help or insite you may provide. If you would
like, I could send you the programm uncompiled whem I'm done if youwould like
to add to your download section.
                                          Steve Fekete

Answer by Martin Allen:

I will try to answer it as best as I can, but if you still want more information, do not hesitate to contact me.

Undo command: if you want an undo command, the best way to do it is to 'send' key strokes to the textbox that you want to use. You will need to send Ctrl+Z. You can do this with this code:

'General Declarations
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const KEYEVENTF_KEYUP = &H2

Private Sub Command1_Click() 'The procedure that will run the undo command
Text1.SetFocus 'The textbox that you want to 'undo'
'Send Ctrl+Z
keybd_event 17, 0, 0, 0
keybd_event 90, 0, 0, 0
'Release Ctrl+Z
keybd_event 90, 0, KEYEVENTF_KEYUP, 0
keybd_event 17, 0, KEYEVENTF_KEYUP, 0
End Sub

Another way to do this is to use the SendKeys statement. However, the API function gives you more control.

Private Sub Command1_Click() 'The procedure that will run the undo command
Text1.SetFocus 'The textbox that you want to 'undo'
'Send Ctrl+Z
SendKeys "^Z"
End Sub

Date: to insert the date:

Text1.SelText = Format(Date)

To insert your name:

Text1.SelText = "Steve Fekete"

The best way to store a program's settings is to use the registry. To do this in Visual Basic, you use the SaveSetting method to save settings and the GetSetting method to get them back again.


'Save the contents of Text1.Text in the registry.
SaveSetting "JavaPad", "Settings", "Name", Text1.Text


'Set the contents of Text1.Text to the name previously stored.
Text1.Text = GetSetting("JavaPad", "Settings", "Name")

I would be interested to receive an uncompiled version of your program, please reply and tell me how you got on with the above help.

P.S. I did not know about the undo command so that will help me as well, I found out by looking at something on and adapted it for an undo command.

Archived Comments

From: Crispy B
Date: Monday, January 24, 2011 at 06:35:40

try this one code for undo function..

Private Sub AddHistory()
    ' Add to history array
    If (lblResult <> LastAct(HistIdx)) Then
        HistIdx = (HistIdx + 1) Mod 20
        LastAct(HistIdx) = lblResult
        LastAct((HistIdx + 1) Mod 20) = sZERO
    End If
End Sub

Private Sub cmdretrieve_Click(index As Integer' Undo history up to 20 levels
    If (index = 1) Then
        If (LastAct((HistIdx + 1) Mod 20) <> sZERO) Then
            HistIdx = (HistIdx + 1) Mod 20  ' +1
        End If
(LastAct((HistIdx + 19) Mod 20) <> sZERO) Then
            HistIdx = (HistIdx + 19) Mod 20 ' -1
        End If
    End If

    lblResult = LastAct(HistIdx)
End Sub

Private Sub Form_Load()
    'load setting
    Store = sZERO
    Dim Idx As Integer
    ' Init undo histroy array
    For Idx = 0 To 19
        LastAct(Idx) = sZERO
    Next Idx
    ' Default to always-on-top

Martin Allen 1999 - 2011.  Last updated Thursday 11 August 2011 07:55:32 PM +0100.