Clipboard and Undo
Cuts, copies and pastes the selection of a RichTextBox, and takes changes back and puts them back again.
RichTextBox1.Text = "ab"
RichTextBox1.SelStart = 2
RtbUndoSendMessage RichTextBox1.hWnd, &H102, AscW("c"), 0 ' WM_CHAR, as a key would
RtbUndoSendMessage RichTextBox1.hWnd, &H102, AscW("d"), 0
Debug.Print RichTextBox1.Text; RichTextBox1.UndoType = RtfActionTypeTyping
RichTextBox1.Undo
Debug.Print RichTextBox1.Text; RichTextBox1.CanRedo
RichTextBox1.Redo
Debug.Print RichTextBox1.Text
' Output:
' abcdTrue
' abTrue
' abcd
RtbUndoSendMessage in the samples of this page is the Windows function SendMessageW, declared by the program.
The clipboard
Copy, Cut, Paste and Clear do to the selection what they do in a TextBoxW, with the formatting: what the control copies is in the Rich Text Format and as plain text, and what it pastes keeps its format when the clipboard holds RTF. CanPaste says whether the clipboard holds something that the control can paste.
PasteSpecial pastes the clipboard in one format of the program’s choice, such as vbCFText for the text without its formatting. PasteSpecialDlg shows the Paste Special dialog box of Windows, in which the user chooses the format. The user has the same two with the keys: Ctrl+Shift+V pastes the text without its formatting, and Ctrl+Alt+V shows the dialog box.
If RichTextBox1.CanPaste(vbCFText) Then
RichTextBox1.PasteSpecial vbCFText ' the text, not its fonts and colors
End If
Undo and redo
Undo takes back the last action, and Redo puts back what Undo took back. Unlike the undo of a TextBoxW, the control keeps many actions, up to UndoLimit, and takes them back one after the other. UndoType and RedoType say what kind of action the next Undo and Redo would take back or put back, and CanUndo and CanRedo whether there is one.
Characters typed one after the other are one action. StopUndoAction ends that action, so that what is typed after it is taken back on its own.
RichTextBox1.Text = ""
RtbUndoSendMessage RichTextBox1.hWnd, &H102, AscW("a"), 0
RtbUndoSendMessage RichTextBox1.hWnd, &H102, AscW("b"), 0
RichTextBox1.StopUndoAction
RtbUndoSendMessage RichTextBox1.hWnd, &H102, AscW("c"), 0
RichTextBox1.Undo
Debug.Print RichTextBox1.Text ' "c" is taken back alone
' Output:
' ab
Properties
RedoType
The kind of action that Redo would put back. A member of RtfActionTypeConstants, read-only. It has a meaning while CanRedo returns True.
UndoLimit
The greatest number of actions that Undo can take back. Long. Default: 100. A value greater than 1000 sets 1000.
With 0 the control keeps no action, so there is nothing to take back. A negative value raises run-time error 380.
UndoType
The kind of action that Undo would take back. A member of RtfActionTypeConstants, read-only. It has a meaning while CanUndo returns True.
Methods
CanPaste
Returns whether the clipboard holds something that the control can paste.
Syntax: object.CanPaste( [ wFormat ] ) As Boolean
- wFormat
- optional A Long: a format of the clipboard, such as a member of ClipboardConstants, to ask about that format alone. With 0, the default, any format that the control can paste counts.
CanRedo
Returns whether Redo has an action to put back.
Syntax: object.CanRedo As Boolean
CanUndo
Returns whether Undo has an action to take back.
Syntax: object.CanUndo As Boolean
Clear
Deletes the selection.
Syntax: object.Clear
Copy
Copies the selection to the clipboard, as RTF and as plain text.
Syntax: object.Copy
Cut
Copies the selection to the clipboard and deletes it from the box.
Syntax: object.Cut
Paste
Puts what the clipboard holds in place of the selection, or at the insertion point.
Syntax: object.Paste
PasteSpecial
Puts what the clipboard holds in one format in place of the selection, or at the insertion point.
Syntax: object.PasteSpecial wFormat
- wFormat
- required A Long: the format, such as vbCFText, vbCFRTF or vbCFBitmap, members of ClipboardConstants, or 13 for Unicode text.
PasteSpecialDlg
Shows the Paste Special dialog box of Windows with the formats of the clipboard that the control can paste, and pastes in the format that the user chooses. The names of the formats are in the language of Windows. Nothing is pasted when the user cancels.
Syntax: object.PasteSpecialDlg
Redo
Puts back the last action that Undo took back.
Syntax: object.Redo
ResetUndoQueue
Forgets the actions that Undo and Redo would take back and put back, so that CanUndo returns False.
Syntax: object.ResetUndoQueue
StopUndoAction
Ends the action of typing that is going on, so that what is typed next is an action of its own. See Undo and redo.
Syntax: object.StopUndoAction
Undo
Takes back the last action: what the user typed, deleted, pasted or dragged, or an assignment to SelText.
Syntax: object.Undo
RtfActionTypeConstants
The values of UndoType and RedoType.
| Member | Value | Description |
|---|---|---|
| RtfActionTypeUnknown | 0 | Another kind of action, or none. |
| RtfActionTypeTyping | 1 | Typing. |
| RtfActionTypeDelete | 2 | Deleting. |
| RtfActionTypeDragDrop | 3 | Dragging and dropping. |
| RtfActionTypeCut | 4 | Cutting. |
| RtfActionTypePaste | 5 | Pasting. |
| RtfActionTypeAutoTable | 6 | A table that the control made itself as the user typed. |
See Also
- RichTextBox control
- Selection – what the clipboard methods work on
- ClipboardConstants – the formats of the clipboard