EM_UNDO Message

Platforms

Description & Usage

Sending the EM_UNDO message to an edit control undoes the last change made to the control's text. Most changes to an edit control can be undone. However, programmatically setting the text (such as using WM_SETTEXT, for example) cannot be undone. On the other hand, though, undo operations themselves can be undone. The EM_UNDO message has the same effect as selecting "Undo" from the edit control's context menu.

Return Value

If the edit control is a single-line edit control, the message always returns a nonzero value.

If the edit control is a multi-line edit control, the message returns a nonzero value if the undo was successful and zero if the undo failed.

Visual Basic-Specific Issues

None.

Parameters

wParam
Not used -- set to 0.
lParam
Not used -- set to 0.

Constant Definitions

Const EM_UNDO = &HC7

Example

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

See Also

EM_CANUNDO

Category

Edit Controls

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_undo.html