Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Aug 19, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Undo Changes in Text Boxes

Most professional applications have an Undo menu option under the Edit menu. If you want to have an undo feature for every text box, you may think you need to create a variable to hold the old value for every field. Fortunately, Windows takes care of this for you. With a call to the Windows API function SendMessage(), you can have the old value put back into the text box. Create a menu item called Undo and add this code:
 Sub mnuUndo_Click () 
Call TextUndo(Screen.ActiveControl) 
End Sub 

TextUndo() is responsible for performing the undo: 

Declare Function SendMessage Lib "User" _ 
(ByVal hWnd As Integer, ByVal wMsg As Integer, _ 
ByVal wParam As Integer, lParam As Any) As Long 

Sub TextUndo (ctlControl As Control) 
Dim lReturn As Long 

Const EM_UNDO = &H417 
If TypeOf Screen.ActiveControl Is TextBox Then 
lReturn = SendMessage(ctlControl.hWnd, _ 
EM_UNDO, 0, 0&) 
End If 
End Sub
Because some third-party controls also support the undo message, place TextUndo() in a separate routine to make it easy to change this one function by adding the Class name of the third-party control. If you do, there will be no reason to change all the calls to the SendMessage() function.
Paul D. Sheriff
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date