martin2k

Google
 
Web www.martin2k.co.uk

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

Question


Home 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 Contact


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.

E.g.

'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 http://www.vb-helper.com and adapted it for an undo command.


Please fill in the below form if you have any comments or additional information you want to add about the above information.  If you have any Visual Basic 6.0 questions, please visit the ForumAny questions sent using this form will be ignored.

* - Mandatory

*Name: E-mail:

*Comments:

*Please type the following code (your comment will not be accepted without it):


Martin Allen 1999 - 2005.  Last updated Tuesday 16 August 2005 10:28:42 PM +0100.