How to undo a textbox

Applies To

OS:
VB:
NT, 9x, 2000
4, 5, 6

Code below provides the following functionality:

Project Instructions
  1. Create a new standard project
  2. Add 1 textbox and 3 command buttons.
  3. Set the Caption of the first command button to Undo
  4. Set the Caption of the second command button to Empty Undo Buffer
  5. Set the Caption of the third command button to Can be undone?
Add the following to a Form
Private Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, lParam As Any) As Long
Private Const EM_CANUNDO = &HC6
Private Const EM_EMPTYUNDOBUFFER = &HCD
Private Const EM_UNDO = &HC7

Private Sub Command1_Click()
    SendMessage Text1.hwnd, EM_UNDO, 0&, 0&
End Sub

Private Sub Command2_Click()
    SendMessage Text1.hwnd, EM_EMPTYUNDOBUFFER, 0&, 0&
End Sub

Private Sub Command3_Click()
    Dim retcode As Long
    
    retcode = SendMessage(Text1.hwnd, EM_CANUNDO, 0&, 0&)
    If retcode Then
        MsgBox "The text box can be UNDOne"
    Else
        MsgBox "The text box cannot be UNDOne"
    End If
End Sub
Remarks

Run the project, type something into the text box and test out various functions.