# Clipboard Operations

> Add Cut, Copy and Paste to a Windows Forms menu with CanCopy, CanPaste and GetClipboardFormats, paste plain text only and load RTF with its page size.

- **Product:** TX Text Control .NET for Windows Forms
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for Windows Forms > Document Editor > Editing Text > Clipboard Operations
- **2263 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/clipboard/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/clipboard/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/clipboard/llms-full.txt

---

Users of a word processor expect to copy text into other applications and to paste content from Word, a browser or an image editor. `TextControl` exchanges text, formatted text and images with the Windows clipboard on its own. With a few methods, your application adds its own commands, pastes only plain text or checks what the clipboard holds.

The examples extend the form of [Create Your First Windows Forms Application](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-editor/first-application/llms-full.txt), whose editor is named `textControl1`. What gets copied is the current selection, described on [Selections and Caret Position](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/selections/llms-full.txt).

### What Works Without Code

Ctrl+C copies the selected text to the clipboard, Ctrl+X cuts it and Ctrl+V pastes the content of the clipboard in place of the selection, without any code. [Keyboard Shortcuts](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/keyboard-shortcuts/#edit-text) lists these keys with all others of the editor.

With the ribbon of the tutorial, the **Home** tab offers the same commands in its **Clipboard** group. The **Paste** button has a drop-down menu with separate paste commands for text, plain text, images, text frames, charts, barcodes and drawings.

![The Home tab of the Windows Forms ribbon with the Paste, Cut and Copy buttons in the Clipboard group and Find, Replace, Go to and Select All in the Editing group.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/ribbon-home-tab.31fd888c.webp)

When the clipboard holds data in several formats, the editor pastes the richest one. The order is: text frame, formatted image, formatted text in the TX Text Control format, RTF, HTML, plain text and image. Text copied in Word, for example, is pasted as RTF with its formatting.

### Copy, Cut and Paste in Code

| Member | Does |
|---|---|
| [Copy](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/copy-method/llms-full.txt) | Copies the selection to the clipboard |
| [Cut](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/cut-method/llms-full.txt) | Moves the selection to the clipboard |
| [Paste](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/paste-method/llms-full.txt) | Replaces the selection with the content of the clipboard; with a [ClipboardFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/clipboardformat/enumeration/llms-full.txt) value, it pastes only that format |
| [CanCopy](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/cancopy-property/llms-full.txt) | Whether text is selected and may be copied |
| [CanPaste](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/canpaste-property/llms-full.txt) | Whether the clipboard holds a format the editor can paste, and the document may be changed |
| [GetClipboardFormats](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/getclipboardformats-method/llms-full.txt) | The formats on the clipboard that the editor can paste, or `null` if there are none |

An **Edit** menu calls `Cut`, `Copy` and `Paste` from its items and enables them each time it opens, as [Integrating Existing Menus and Toolbars](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/integration/menus-toolbars/llms-full.txt) builds it with a `MenuStrip`. Two details matter for the enabled states: `CanCopy` is `false` when nothing is selected, and cutting also needs [CanEdit](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/canedit-property/llms-full.txt), which is `false` when the document is read-only. The clipboard can change while your application is in the background, so check the states when the menu opens rather than once.

A **Paste as Plain Text** item pastes text without formatting. `Paste(ClipboardFormat.PlainText)` requires plain text on the clipboard, so the item is enabled only when the array that `GetClipboardFormats` returns contains it:

```csharp
private void pastePlainTextItem_Click(object sender, EventArgs e)
{
    textControl1.Paste(ClipboardFormat.PlainText);
}

private void EditMenu_DropDownOpening(object sender, EventArgs e)
{
    // Paste(ClipboardFormat) accepts only a format the clipboard offers
    ClipboardFormat[] formats = textControl1.GetClipboardFormats();
    pastePlainTextItem.Enabled = textControl1.CanPaste && formats != null
        && Array.IndexOf(formats, ClipboardFormat.PlainText) >= 0;
}
```

`pastePlainTextItem` is a `ToolStripMenuItem` of the **Edit** menu, whose `DropDownOpening` event is connected to the handler. Copy formatted text from another application and open **Edit**: **Paste** inserts it with its formatting, **Paste as Plain Text** without.

### Values of ClipboardFormat

| Value | Clipboard content |
|---|---|
| `TXTextControlFormat` | Formatted text in the internal TX Text Control format |
| `RichTextFormat` | RTF |
| `HTMLFormat` | HTML |
| `PlainText` | Text without formatting |
| `Image` | A device-dependent or device-independent bitmap, a TIFF image or a Windows metafile |
| `TXTextControlImage` | An image with its position in the document, inline or geometrically positioned |
| `TXTextControlTextframe` | A text frame with its content and position |
| `Chart`, `Barcode` | A chart or a barcode |

Pasting a specific format is how a "Paste Special" command is built: list the values that `GetClipboardFormats` returns and call `Paste` with the one the user picks. The blog article [Paste Special: The Easy Way to Implement](https://www.textcontrol.com/blog/2009/09/07/paste-special-the-easy-way-to-implement/) describes the idea.

### When Copying and Pasting Is Not Allowed

`CanCopy` and `CanPaste` take the protection of the document into account:

- [EditMode](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/editmode-property/llms-full.txt) `ReadOnly` shows the text without allowing a selection, so nothing can be copied. With `ReadAndSelect`, text can be selected, and the document permissions decide what else is allowed.
- [DocumentPermissions.AllowCopy](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/documentpermissions/allowcopy-property/llms-full.txt) set to `false` prevents copying in a protected document. [DocumentPermissions](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/documentpermissions-property/llms-full.txt) returns the permissions of the loaded document.

[Document Protection and Editable Regions](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/protection/llms-full.txt) explains both.

### Delete the Selection

[Clear](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/clear-method/llms-full.txt) deletes the selected text, or the character to the right of the input position if nothing is selected, without touching the clipboard. It works only while the editor has the input focus; call `textControl1.Focus()` first when a button of your form has taken it.

### Paste a Word Document With Its Page Size

Pasted text flows into the pages of the current document: the page size and margins of the document it was copied from are not taken over. To open copied content as a document with its own page settings instead, load the RTF data from the clipboard with [Load](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/load-method/llms-full.txt) and a [LoadSettings](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/loadsettings/llms-full.txt) object:

```csharp
private void PasteAsDocument()
{
    string rtf = Clipboard.GetData(DataFormats.Rtf) as string;

    if (rtf != null)
    {
        LoadSettings loadSettings = new LoadSettings();
        textControl1.Load(rtf, StringStreamType.RichTextFormat, loadSettings);

        // Take over the page size and margins of the copied document
        textControl1.PageSize = loadSettings.PageSize;
        textControl1.PageMargins = loadSettings.PageMargins;
    }
}
```

`Clipboard` and `DataFormats` are the Windows Forms classes of `System.Windows.Forms`. `Load` replaces the whole document, and [LoadSettings.PageSize](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/loadsettings/pagesize-property/llms-full.txt) and [PageMargins](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/loadsettings/pagemargins-property/llms-full.txt) return the page settings read from the data, which [TextControl.PageSize](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/pagesize-property/llms-full.txt) and [TextControl.PageMargins](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/pagemargins-property/llms-full.txt) apply to the editor.

### Next Steps

- [Undo and Redo](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/undo-redo/llms-full.txt): undo a paste.
- [Drag and Drop](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/drag-drop/llms-full.txt): move text with the mouse and accept dropped files.
- [Context Menus](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/context-menus/llms-full.txt): the built-in context menu of the editor.
- [Keyboard Shortcuts](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/keyboard-shortcuts/llms-full.txt): all keys of the editor.

---

## Nearby

- **Previous:** [Selections and Caret Position](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/selections/llms-full.txt)
- **Next:** [Undo and Redo](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/undo-redo/llms-full.txt)
