# undo/redo in web browser



## LIL L (Apr 9, 2008)

i'm making a web browser in vb.net using vs 2008 pro. i want to have the the edit menu have undo/redo/cut/copy/paste/delete. i managed to find some code for that and updated it to 2008 as much as possible but i'm getting some errors. can anyone help me out?


for my form1_load i put:

```
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'undo and redo code
        Dim lStyle As Long
        '// required to 'reveal' multiple undo
        '// set rich text box style
        lStyle = TM_RICHTEXT Or TM_MULTILEVELUNDO Or TM_MULTICODEPAGE
        SendMessageLong(rtfText.hwnd, EM_SETTEXTMODE, lStyle, 0)
```

i called my Update items:

```
Private Sub mnuEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuEdit.Click
        Call UpdateItems()
    End Sub
```
i put all of this under end sub of my mnuEdit_Click

```
Public Property UndoType() As ERECUndoTypeConstants
        Get
            UndoType = SendMessageLong(rtfText.hWnd, EM_GETUNDONAME, 0, 0)
        End Get
        Set(ByVal value As ERECUndoTypeConstants)
            SendMessageLong(rtfText.hWnd, EM_UNDO, 0, 0) = value
        End Set
    End Property
    Public Property RedoType() As ERECUndoTypeConstants
        Get
            RedoType = SendMessageLong(rtfText.hWnd, EM_GETREDONAME, 0, 0)
        End Get
        Set(ByVal value As ERECUndoTypeConstants)
            SendMessageLong(rtfText.hWnd, EM_REDO, 0, 0) = value
        End Set
    End Property
    Public Property CanPaste() As Boolean
        Get
            CanPaste = SendMessageLong(rtfText.hWnd, EM_CANPASTE, 0, 0)
        End Get
        Set(ByVal value As Boolean)
            SendMessageLong(rtfText.hWnd, WM_PASTE, 0, 0) = value
        End Set
    End Property
    Public Property CanCopy() As Boolean
        Get
            If rtfText.SelLength > 0 Then
                CanCopy = True
            End If
        End Get
        Set(ByVal value As Boolean)
            SendMessageLong(rtfText.hWnd, WM_COPY, 0, 0)
        End Set
    End Property
    Public Property CanUndo() As Boolean
        Get
            CanUndo = SendMessageLong(rtfText.hWnd, EM_CANUNDO, 0, 0)
        End Get
        Set(ByVal value As Boolean)
            SendMessageLong(rtfText.hWnd, EM_UNDO, 0, 0) = value
        End Set
    End Property
    Public Property CanRedo() As Boolean
        Get
            CanRedo = SendMessageLong(rtfText.hWnd, EM_CANREDO, 0, 0)
        End Get
        Set(ByVal value As Boolean)
            SendMessageLong(rtfText.hWnd, EM_REDO, 0, 0) = value
        End Set
    End Property

    '///////////////////////////////////////////////////////
    '// Methods
    Public Sub Undo()
        SendMessageLong(rtfText.hWnd, EM_UNDO, 0, 0)
    End Sub
    Public Sub Redo()
        SendMessageLong(rtfText.hWnd, EM_REDO, 0, 0)
    End Sub
    Public Sub Cut()
        SendMessageLong(rtfText.hWnd, WM_CUT, 0, 0)
    End Sub
    Public Sub Copy()
        SendMessageLong(rtfText.hWnd, WM_COPY, 0, 0)
    End Sub
    Public Sub Paste()
        SendMessageLong(rtfText.hWnd, WM_PASTE, 0, 0)
    End Sub
    Public Sub Clear()
        rtfText.SelText = Nothing
    End Sub
    Public Sub UpdateItems()
        Dim bCanUndo As Boolean
        '// Undo/Redo options:
        bCanUndo = CanUndo
        mnuEdit.Enabled = bCanUndo
        '// Set Undo Text
        If (bCanUndo) Then
            mnuEdit.Text = "&Undo " & TranslateUndoType(UndoType)
        Else
            mnuEdit.Text = "&Undo"
        End If
        '// Set Redo Text
        bCanUndo = CanRedo
        If (bCanUndo) Then
            mnuRedo.Text = "&Redo " & TranslateUndoType(RedoType)
        Else
            mnuRedo.Text = "&Redo"
        End If
        mnuRedo.Enabled = bCanUndo

        '// Cut/Copy/Paste/Clear options
        mnuCut.Enabled = CanCopy
        mnuCopy.Enabled = CanCopy
        mnuPaste.Enabled = CanPaste
        mnuDelete.Enabled = CanCopy
    End Sub
    '// Returns the undo/redo type
    Private Function TranslateUndoType(ByVal eType As ERECUndoTypeConstants) As String
        Select Case eType
            Case ercUID_UNKNOWN
                TranslateUndoType = "Last Action"
            Case ercUID_TYPING
                TranslateUndoType = "Typing"
            Case ercUID_PASTE
                TranslateUndoType = "Paste"
            Case ercUID_DRAGDROP
                TranslateUndoType = "Drag Drop"
            Case ercUID_DELETE
                TranslateUndoType = "Delete"
            Case ercUID_CUT
                TranslateUndoType = "Cut"
        End Select
    End Function
```
this is what i put in my module:

```
Module UndoRedo

    '// View Types
    Public Enum ERECViewModes
        ercDefault = 0
        ercWordWrap = 1
        ercWYSIWYG = 2
    End Enum
    '// Undo Types
    Public Enum ERECUndoTypeConstants
        ercUID_UNKNOWN = 0
        ercUID_TYPING = 1
        ercUID_DELETE = 2
        ercUID_DRAGDROP = 3
        ercUID_CUT = 4
        ercUID_PASTE = 5
    End Enum
    '// Text Modes
    Public Enum TextMode
        TM_PLAINTEXT = 1
        TM_RICHTEXT = 2 ' /* default behavior */
        TM_SINGLELEVELUNDO = 4
        TM_MULTILEVELUNDO = 8 ' /* default behavior */
        TM_SINGLECODEPAGE = 16
        TM_MULTICODEPAGE = 32 ' /* default behavior */
    End Enum

    Public Const WM_COPY = &H301
    Public Const WM_CUT = &H300
    Public Const WM_PASTE = &H302

    Public Const WM_USER = &H400
    Public Const EM_SETTEXTMODE = (WM_USER + 89)
    Public Const EM_UNDO = &HC7
    Public Const EM_REDO = (WM_USER + 84)
    Public Const EM_CANPASTE = (WM_USER + 50)
    Public Const EM_CANUNDO = &HC6&
    Public Const EM_CANREDO = (WM_USER + 85)
    Public Const EM_GETUNDONAME = (WM_USER + 86)
    Public Const EM_GETREDONAME = (WM_USER + 87)

    Public Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
End Module
```
these are all my errors:

```
Error	31	'As Any' is not supported in 'Declare' statements.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\UndoRedo.vb	43	154	browser
Error	8	'RedoType' cannot expose type 'UndoRedo.ERECUndoTypeConstants' outside the project through class 'Form1'.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	350	35	browser
Error	5	'UndoType' cannot expose type 'UndoRedo.ERECUndoTypeConstants' outside the project through class 'Form1'.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	342	35	browser
Error	30	Name 'ercUID_CUT' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	452	18	browser
Error	29	Name 'ercUID_DELETE' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	450	18	browser
Error	28	Name 'ercUID_DRAGDROP' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	448	18	browser
Error	27	Name 'ercUID_PASTE' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	446	18	browser
Error	26	Name 'ercUID_TYPING' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	444	18	browser
Error	25	Name 'ercUID_UNKNOWN' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	442	18	browser
Error	4	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	12	25	browser
Error	6	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	344	40	browser
Error	7	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	347	29	browser
Error	9	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	352	40	browser
Error	10	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	355	29	browser
Error	11	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	360	40	browser
Error	12	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	363	29	browser
Error	13	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	368	16	browser
Error	14	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	373	29	browser
Error	15	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	378	39	browser
Error	16	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	381	29	browser
Error	17	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	386	39	browser
Error	18	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	389	29	browser
Error	19	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	396	25	browser
Error	20	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	399	25	browser
Error	21	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	402	25	browser
Error	22	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	405	25	browser
Error	23	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	408	25	browser
Error	24	Name 'rtfText' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	411	9	browser
Error	3	Name 'TM_MULTICODEPAGE' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	11	54	browser
Error	2	Name 'TM_MULTILEVELUNDO' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	11	33	browser
Error	1	Name 'TM_RICHTEXT' is not declared.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\browser\browser\Form1.vb	11	18	browser
```


----------

