# Selections, Clipboard and Undo

> Select text and move the input position in VB6, run cut, copy and paste, undo and redo edits, group undo steps, search text and switch overwrite mode.

- **Product:** TX Text Control ActiveX
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control ActiveX > Document Editor > Selections, Clipboard and Undo
- **4882 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/editing/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/editing/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/editing/llms-full.txt

---

Which text does a menu command work on, and how does the user take it back? In `TXTextControl`, commands work on the *selection*, the highlighted text, or on the *input position*, the place where the caret stands when nothing is selected. Cut, copy and paste move the selection through the clipboard, and the undo buffer records each edit so that it can be undone and redone.

The Visual Basic 6 code below comes from the **Edit** menu of the *TXWords* sample, which [A Word Processor](https://docs.textcontrol.com/tx-text-control-activex/document-editor/word-processor/llms-full.txt) describes, and from the reference. It selects and replaces text in code, runs the clipboard commands, enables menu items, undoes edits and groups them, searches the text and switches between inserting and overwriting.

### Select Text in Code

Three run-time properties describe the selection:

- [SelStart](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/selstart-property/llms-full.txt) is the zero-based position where the selection starts, or the input position when no text is selected. A value greater than the text length is limited to the text length, and -1 sets the start to the end of the document.
- [SelLength](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/sellength-property/llms-full.txt) is the number of selected characters, from 0 to the text length. A value larger than the text extends the selection to the end of the document.
- [SelText](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/seltext-property/llms-full.txt) is the selected text as a plain string, or an empty string when nothing is selected. Setting it replaces the selection with the new string and sets `SelLength` to 0.

The following code selects the first five characters, reads them and replaces them:

```vbscript
Dim OldText As String

' Select characters 0 to 4
TXTextControl1.SelStart = 0
TXTextControl1.SelLength = 5
OldText = TXTextControl1.SelText

' Replace the selection with new text
TXTextControl1.SelText = "Hello"
```

`SelText` holds text without formatting. [RTFSelText](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/rtfseltext-property/llms-full.txt) works the same way on the selection that `SelStart` and `SelLength` set, but reads and writes it as an RTF string with all formatting attributes.

#### Select All Text

The **Select All** command of the *TXWords* **Edit** menu (CTRL+A) selects the whole document from position 0:

```vbscript
Private Sub mnuEdit_SelectAll_Click()
    TXTextControl1.SelStart = 0
    TXTextControl1.SelLength = -1
End Sub
```

TXWords sets `SelLength` to -1 here. The reference documents the other way to reach the end of the document: a `SelLength` larger than the text extends the selection to its end.

#### Keep the Selection Visible

By default, the selection is hidden when `TXTextControl` loses the input focus, for instance to a dialog box of your application, and shown again when the control gets the focus back. Set [HideSelection](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/hideselection-property/llms-full.txt) to **False** to keep it visible:

```vbscript
TXTextControl1.HideSelection = False
```

#### Format the Selection

Formatting properties such as [FontBold](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/fontbold-property/llms-full.txt) apply to the complete text by default. Set [FormatSelection](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/formatselection-property/llms-full.txt) to **True**, as TXWords does when its main form loads, to make them apply to the selected text only. [Character and Paragraph Formatting](https://docs.textcontrol.com/tx-text-control-activex/document-editor/formatting/llms-full.txt) covers the formatting properties.

### Work With the Input Position

The input position is a character position, but you can also read and set it as a page, a line and a column. [CurrentInputPosition](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/currentinputposition-property/llms-full.txt) is an array of three `Long` values, the numbers that the status bar shows. Each of them starts at 1:

```vbscript
Dim cur As Variant

' Get the current page, line and column
cur = TXTextControl1.CurrentInputPosition
MsgBox "Page " & cur(0) & ", line " & cur(1) & ", column " & cur(2)

' Move the input position to the start of the text
cur(0) = 1 ' page
cur(1) = 1 ' line
cur(2) = 1 ' column
TXTextControl1.CurrentInputPosition = cur
```

To find the input position at a point of the control, call [InputPosFromPoint](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/inputposfrompoint-method/llms-full.txt) with the coordinates of the point in *twips*, 1/1440 inch, which [Text Formatting and Views](https://docs.textcontrol.com/tx-text-control-activex/document-editor/viewing/#measure-in-twips) explains. The method returns the zero-based input position, or -1 if there is no text position at that point. TXWords places the input position where a file is dropped this way:

```vbscript
frmMain.TXTextControl1.SelStart = frmMain.TXTextControl1.InputPosFromPoint(X, Y)
```

The [PosChange](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/poschange-event/llms-full.txt) event occurs whenever the input position changes. TXWords uses it to enable and disable its toolbar buttons, as the next section shows.

### Cut, Copy and Paste

One method runs all clipboard commands: [Clip](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/clip-method/llms-full.txt), with the action as its parameter. TXWords declares a constant for each action in its module *tx.bas*:

| Value | TXWords constant | Action |
|---|---|---|
| 1 | `CLIP_CUT` | Cuts out the selected text and copies it to the clipboard. |
| 2 | `CLIP_COPY` | Copies the selected text to the clipboard. |
| 3 | `CLIP_PASTE` | Pastes text from the clipboard. |
| 4 | `CLIP_CLEAR` | Clears the selection. |

The **Cut**, **Copy**, **Paste** and **Delete** commands of the TXWords **Edit** menu each call `Clip`:

```vbscript
Private Sub mnuEdit_Cut_Click()
    TXTextControl1.Clip CLIP_CUT
End Sub

Private Sub mnuEdit_Copy_Click()
    TXTextControl1.Clip CLIP_COPY
End Sub

Private Sub mnuEdit_Paste_Click()
    TXTextControl1.Clip CLIP_PASTE
End Sub

Private Sub mnuEdit_Delete_Click()
    TXTextControl1.Clip CLIP_CLEAR
End Sub
```

#### Enable the Clipboard Commands

A clipboard command makes sense only when there is something to cut, copy or paste. Two read-only properties tell you:

- [CanCopy](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/cancopy-property/llms-full.txt) is **True** when part of the document is selected and can be copied.
- [CanPaste](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/canpaste-property/llms-full.txt) is **True** when the clipboard holds a format that can be pasted into the document.

TXWords checks both each time the input position changes and enables its toolbar buttons accordingly. Cutting also needs [CanEdit](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/canedit-property/llms-full.txt), which is **True** when the text at the input position can be changed; in a protected document it can be **False**:

```vbscript
Private Sub TXTextControl1_PosChange()
    With Toolbar1
       .Buttons("Cut").Enabled = TXTextControl1.CanCopy And TXTextControl1.CanEdit
       .Buttons("Copy").Enabled = TXTextControl1.CanCopy
       .Buttons("Paste").Enabled = TXTextControl1.CanPaste
    End With
End Sub
```

TXWords calls a procedure named `EnableToolbarButtons` from the `PosChange` event, which enables more buttons than this excerpt. [Document Protection](https://docs.textcontrol.com/tx-text-control-activex/document-editor/protection/llms-full.txt) explains the edit modes and permissions behind `CanEdit`.

### Undo and Redo

The *undo buffer* of `TXTextControl` records the edits of the user and of your code. The [Undo](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/undo-method/llms-full.txt) method undoes the last operation and [Redo](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/redo-method/llms-full.txt) redoes it. Both return **True** when they succeed. The keys CTRL+Z and CTRL+Y do the same for the user.

```vbscript
Private Sub mnuEdit_Undo_Click()
    TXTextControl1.Undo
End Sub

Private Sub mnuEdit_Redo_Click()
    TXTextControl1.Redo
End Sub
```

#### Name the Operation in the Menu

[CanUndo](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/canundo-property/llms-full.txt) and [CanRedo](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/canredo-property/llms-full.txt) return 0 when there is nothing to undo or redo. Otherwise they return a number that identifies the operation, such as 1 for inserted text, 19 for an inserted image or 23 for an inserted table; the reference pages list all numbers. For inserted, deleted and formatted text, the two properties use different numbers: `CanUndo` returns 1, 2 and 3, `CanRedo` returns 10, 11 and 12.

TXWords turns the number into a menu caption such as "Undo insert text" with the functions `UndoActionName` and `RedoActionName` of *tx.bas*, and it disables the commands when the number is 0. It does this in the `Click` event of the **Edit** menu, which occurs when the user opens the menu:

```vbscript
Private Sub mnuEdit_Click()
    Dim UndoState As Integer
    Dim RedoState As Integer

    With TXTextControl1
        UndoState = .CanUndo
        RedoState = .CanRedo

        mnuEdit_Undo.Caption = UndoActionName(UndoState)
        mnuEdit_Redo.Caption = RedoActionName(RedoState)

        mnuEdit_Undo.Enabled = (UndoState <> 0)
        mnuEdit_Redo.Enabled = (RedoState <> 0)

        mnuEdit_Cut.Enabled = .CanCopy And .CanEdit
        mnuEdit_Copy.Enabled = .CanCopy
        mnuEdit_Paste.Enabled = .CanPaste
    End With
End Sub
```

![The TXWords Edit menu of an unchanged document, with the captions Nothing to undo and Nothing to redo and the disabled Cut, Copy and Paste items.](https://docs.textcontrol.com/assets/img/tx-text-control-activex/edit-menu.bea47478.webp)

#### Group Several Changes Into One Undo Step

When your code changes the document with several property and method calls, the user would have to undo each of them separately. [StartUndoAction](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/startundoaction-method/llms-full.txt) and [EndUndoAction](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/endundoaction-method/llms-full.txt) define a single undo action: all calls between them are undone in one step.

```vbscript
With TXTextControl1
    .FormatSelection = True

    .StartUndoAction
    ' Insert a greeting at the start of the text
    .SelStart = 0
    .SelLength = 0
    .SelText = "Dear Customer"
    ' Make it bold
    .SelStart = 0
    .SelLength = Len("Dear Customer")
    .FontBold = 1
    .EndUndoAction
End With
```

After this code runs, one `Undo` removes both the inserted text and its bold formatting.

#### Switch the Undo Buffer Off

[AllowUndo](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/allowundo-property/llms-full.txt) switches the undo buffer on and off. It is on by default, and off by default when the control runs on a server. When you use a `TXTextControl` to generate reports, switching it off saves time and memory. TXWords switches it off while its main form sets up the control and on again afterward:

```vbscript
TXTextControl1.AllowUndo = False ' Disable undo buffer

' Set options
TXTextControl1.ScrollBars = 3 ' Both horizontal and vertical
TXTextControl1.FormatSelection = True ' Formatting options only apply to selected text

TXTextControl1.AllowUndo = True ' Enable undo buffer
```

### Search the Text

The [Find](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/find-method/llms-full.txt) method searches the document for a string. It returns the zero-based position of the match and highlights it, or returns -1 when the string is not found. The optional second parameter is the position to start at; when you omit it or pass -1, the search starts at the input position. The optional third parameter is the sum of these options:

| Value | Option | Effect |
|---|---|---|
| 1 | SearchUp | Searches up instead of down. |
| 4 | MatchCase | Matches the case of the string as well. |
| 8 | NoHighLight | Controls whether the match is highlighted. |
| 16 | NoMessageBox | Suppresses the built-in message box that says a match could not be found. |
| 64 | MatchWholeWord | Finds whole words only. |

The following code searches the whole document for the whole word "invoice", with matching case and without a message box:

```vbscript
Dim Pos As Long

Pos = TXTextControl1.Find("invoice", 0, 4 + 16 + 64)
If Pos = -1 Then
    MsgBox "The word was not found."
End If
```

For a search and replace dialog box, you don't need code of your own. The **Find** and **Replace** commands of TXWords call [FindReplace](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/findreplace-method/llms-full.txt) with 1 or 2, and `TXTextControl` opens its built-in dialog box; [A Word Processor](https://docs.textcontrol.com/tx-text-control-activex/document-editor/word-processor/#search-and-replace-text) shows it.

![The built-in Find dialog box of TXTextControl with the Find what box, the options Match case, Match whole word only and Search up, and the parts of the document to search in.](https://docs.textcontrol.com/assets/img/tx-text-control-activex/find-dialog.9a2cdb25.webp)

![The built-in Replace dialog box of TXTextControl with the Find what and Replace with boxes, the same find options and the buttons Find Next, Replace and Replace All.](https://docs.textcontrol.com/assets/img/tx-text-control-activex/replace-find-options.962b48bb.webp)

### Switch Between Insert and Overwrite

[InsertionMode](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/insertionmode-property/llms-full.txt) decides whether typed text is inserted, the default (**True**), or overwrites the existing text (**False**). TXWords toggles it with the INSERT key in the `KeyDown` event:

```vbscript
Private Sub TXTextControl1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyInsert And Shift = 0 Then
        TXTextControl1.InsertionMode = Not TXTextControl1.InsertionMode
    End If
End Sub
```

The [KeyStateChange](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/keystatechange-event/llms-full.txt) event occurs when the insertion mode or the state of the NUMLOCK or CAPSLOCK key changes.

### Use the Built-In Mouse and Keyboard Interface

Users select, edit and move text without any code of yours. A double-click selects a word, a triple-click a paragraph, and SHIFT + click extends the selection to the point of the click. Dragging selected text moves it, and dragging it with CTRL pressed copies it. SHIFT together with a key that moves the caret, such as SHIFT + END, extends the selection to the new caret position.

[Mouse Assignment](https://docs.textcontrol.com/tx-text-control-activex/reference/assignments/mouse/llms-full.txt) and [Keyboard Assignment](https://docs.textcontrol.com/tx-text-control-activex/reference/assignments/keyboard/llms-full.txt) list every mouse action and key and what the control does. For your own reactions to the mouse and the keyboard, `TXTextControl` raises the events of the programming category [Text Selections and Character Input](https://docs.textcontrol.com/tx-text-control-activex/reference/categories/selections/llms-full.txt), such as [MouseDown](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/mousedown-event/llms-full.txt), [MouseUp](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/mouseup-event/llms-full.txt), [KeyDown](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/keydown-event/llms-full.txt) and [KeyPress](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/keypress-event/llms-full.txt).

### Key Members

| Member | Description |
|---|---|
| [SelStart](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/selstart-property/llms-full.txt) | The zero-based start of the selection, or the input position. |
| [SelLength](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/sellength-property/llms-full.txt) | The number of selected characters. |
| [SelText](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/seltext-property/llms-full.txt) | The selected text; setting it replaces the selection. |
| [CurrentInputPosition](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/currentinputposition-property/llms-full.txt) | The page, line and column of the input position. |
| [InputPosFromPoint](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/inputposfrompoint-method/llms-full.txt) | Returns the input position at a point given in twips. |
| [PosChange](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/poschange-event/llms-full.txt) | Occurs when the input position changes. |
| [Clip](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/clip-method/llms-full.txt) | Cuts, copies, pastes or clears the selection. |
| [CanCopy](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/cancopy-property/llms-full.txt) | Tells whether text is selected and can be copied. |
| [CanPaste](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/canpaste-property/llms-full.txt) | Tells whether the clipboard holds a format that can be pasted. |
| [Undo](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/undo-method/llms-full.txt) | Undoes the last operation. |
| [Redo](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/redo-method/llms-full.txt) | Redoes the last undone operation. |
| [CanUndo](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/canundo-property/llms-full.txt) | Returns the operation that `Undo` can undo, or 0. |
| [CanRedo](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/canredo-property/llms-full.txt) | Returns the operation that `Redo` can redo, or 0. |
| [StartUndoAction](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/startundoaction-method/llms-full.txt) | Starts a group of calls that are undone in one step. |
| [EndUndoAction](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/endundoaction-method/llms-full.txt) | Ends the group that `StartUndoAction` started. |
| [AllowUndo](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/allowundo-property/llms-full.txt) | Switches the undo buffer on or off. |
| [Find](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/find-method/llms-full.txt) | Searches the document for a string. |
| [InsertionMode](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/insertionmode-property/llms-full.txt) | Switches between inserting and overwriting text. |

### Related Pages

- [A Word Processor](https://docs.textcontrol.com/tx-text-control-activex/document-editor/word-processor/llms-full.txt): the *TXWords* sample and its **Find** and **Replace** dialog box.
- [Spell Checking](https://docs.textcontrol.com/tx-text-control-activex/document-editor/spell-checking/llms-full.txt): selecting and correcting misspelled words.
- [Character and Paragraph Formatting](https://docs.textcontrol.com/tx-text-control-activex/document-editor/formatting/llms-full.txt): the formatting properties that work on the selection.
- [Document Protection](https://docs.textcontrol.com/tx-text-control-activex/document-editor/protection/llms-full.txt): edit modes and permissions that decide what users can change.
- [Text Selections and Character Input](https://docs.textcontrol.com/tx-text-control-activex/reference/categories/selections/llms-full.txt): all members for the selection, the input position and mouse and keyboard events.
- [Clipboard Operations](https://docs.textcontrol.com/tx-text-control-activex/reference/categories/clipboard/llms-full.txt) and [Undo](https://docs.textcontrol.com/tx-text-control-activex/reference/categories/undo/llms-full.txt): all members for the clipboard and the undo buffer.
- [Mouse and Keyboard Assignment](https://docs.textcontrol.com/tx-text-control-activex/reference/assignments/llms-full.txt): what each mouse action and key does in the control.

---

## Nearby

- **Previous:** [Spell Checking](https://docs.textcontrol.com/tx-text-control-activex/document-editor/spell-checking/llms-full.txt)
- **Next:** [Button Bar, Ruler Bar and Status Bar](https://docs.textcontrol.com/tx-text-control-activex/document-editor/toolbars/llms-full.txt)
