TX Text Control .NET for Windows Forms

Clipboard Operations

Topic

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, whose editor is named textControl1. What gets copied is the current selection, described on Selections and Caret Position.

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 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.

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

MemberDoes
CopyCopies the selection to the clipboard
CutMoves the selection to the clipboard
PasteReplaces the selection with the content of the clipboard; with a ClipboardFormat value, it pastes only that format
CanCopyWhether text is selected and may be copied
CanPasteWhether the clipboard holds a format the editor can paste, and the document may be changed
GetClipboardFormatsThe 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 builds it with a MenuStrip. Two details matter for the enabled states: CanCopy is false when nothing is selected, and cutting also needs CanEdit, 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:

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

ValueClipboard content
TXTextControlFormatFormatted text in the internal TX Text Control format
RichTextFormatRTF
HTMLFormatHTML
PlainTextText without formatting
ImageA device-dependent or device-independent bitmap, a TIFF image or a Windows metafile
TXTextControlImageAn image with its position in the document, inline or geometrically positioned
TXTextControlTextframeA text frame with its content and position
Chart, BarcodeA 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 describes the idea.

When Copying and Pasting Is Not Allowed

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

  • EditMode 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 set to false prevents copying in a protected document. DocumentPermissions returns the permissions of the loaded document.

Document Protection and Editable Regions explains both.

Delete the Selection

Clear 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 and a LoadSettings object:

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 and PageMargins return the page settings read from the data, which TextControl.PageSize and TextControl.PageMargins apply to the editor.

Next Steps