Sending the EM_CANUNDO message to an edit control determines if the last operation can be undone. If an undo is allowed, the user can select "Undo" from the control's context menu, and the EM_UNDO message can also be sent to perform an undo.
If an undo operation is valid, the function returns a nonzero value. If no undo is possible, the function returns 0.
None.
Const EM_CANUNDO = &HC6
Demonstrate undo operations for an edit control. Place three controls on a form window: an edit control (text box) named txtBox, a command button named cmdUndo, and another command button named cmdSet. Pressing cmdSet sets the contents of txtBox to a predetermined string, a change that cannot be undone. Pressing cmdUndo, naturally, undoes the last change made by the user to txtBox. Whenever the contents of txtBox change, the enabled status of cmdUndo is changed so that the button is enabled if and only if an undo is possible.
' This code is licensed according to the terms and conditions listed here.
' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal _
Msg As Long, wParam As Any, lParam As Any) As Long
Public Const EM_CANUNDO = &HC6
Public Const EM_UNDO = &HC7
' *** Place the following code inside a form window. ***
Private Sub Form_Load()
' Make sure cmdUndo is initially disabled.
cmdUndo.Enabled = False
End Sub
Private Sub txtBox_Change()
' Whenever the contents of txtBox change, see if an undo is possible.
Dim possible As Long ' is it possible?
possible = SendMessage(txtBox.hWnd, EM_CANUNDO, ByVal CLng(0), ByVal CLng(0))
' Since 0 = False and anything else = True, we can do this:
cmdUndo.Enabled = possible
End Sub
Private Sub cmdUndo_Click()
' Undo the last change the user made to the contents of txtBox.
Dim retval As Long ' return value
retval = SendMessage(txtBox.hWnd, EM_UNDO, ByVal CLng(0), ByVal CLng(0))
' Since undo operations can themselves be undone, there's no
' reason to send the EM_CANUNDO message here. cmdUndo is already enabled.
End Sub
Private Sub cmdSet_Click()
' Set the contents of txtBox to a predetermined string. This can't be undone
' using the Undo command or the EM_UNDO message.
txtBox.Text = "You can't use Undo until you change this text!"
End Sub
Back to the Message list.
Back to the Reference section.
Last Modified: August 26, 2000
This page is copyright © 2000 Paul Kuliniewicz.
Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/e/em_canundo.html