Clipboard class

The Clipboard class wraps the system clipboard — the Win32 inter-application copy-and-paste surface — and exposes it as a singleton object. Code reads and writes text, queries which formats are currently available, and (eventually — see the picture caveat) reads and writes pictures.

Clipboard is not creatable: there is exactly one instance per process, owned by the runtime and exposed through the Clipboard property on the Global app-object. Code reaches it without qualification:

' Copy
Clipboard.Clear
Clipboard.SetText "Hello, world!"

' Paste
If Clipboard.GetFormat(vbCFText) Then
    txtEditor.Text = Clipboard.GetText()
End If

Formats

Clipboard contents are tagged with a format — text, bitmap, files, rich text, and so on. The ClipboardConstants enum lists the predefined formats:

Constant Value Meaning
vbCFText 1 ANSI plain text.
vbCFBitmap 2 DDB (device-dependent bitmap).
vbCFMetafile 3 Windows metafile (WMF).
vbCFDIB 8 DIB (device-independent bitmap).
vbCFPalette 9 Colour palette.
vbCFUnicodeText 13 UTF-16 plain text.
vbCFEMetafile 14 Enhanced metafile (EMF).
vbCFFiles 15 A list of file paths (CF_HDROP).
vbCFLink &HFFFFBF00 DDE link (legacy OLE-1 link source).
vbCFRTF &HFFFFBF01 Rich Text Format.

The GetText / SetText methods take an optional Format argument constrained to the text-shaped subset (vbCFText, vbCFUnicodeText, vbCFRTF, vbCFLink). The GetData / SetData methods carry pictures, restricted to the bitmap and metafile formats.

Picture data

The picture methods — GetData and SetData — are declared but not yet wired up.

Note

GetData and SetData are reserved for compatibility with VB6; they are not currently implemented in twinBASIC. For picture-clipboard interop, use the Win32 clipboard API (OpenClipboard, GetClipboardData, SetClipboardData, CloseClipboard) directly until the implementation lands.

Clear, GetText, SetText, and GetFormat are all fully functional.

Methods

Clear

Empties the clipboard, removing every format currently on it.

Syntax: object.Clear

GetData

Reads picture data from the clipboard. Returns the result as a stdole.StdPicture.

Syntax: object.GetData( [ Format ] )

Format
optional A member of ClipboardConstants selecting which picture format to retrieve (vbCFBitmap, vbCFDIB, vbCFMetafile, vbCFEMetafile, or vbCFPalette). When omitted, the implementation picks the most descriptive format the clipboard currently holds.

Note

Reserved for compatibility with VB6; not currently implemented in twinBASIC.

GetFormat

Tests whether the clipboard currently contains data in the given format. Returns True if it does, False otherwise.

Syntax: object.GetFormat( Format )

Format
required A member of ClipboardConstants — the format to probe for.
If Clipboard.GetFormat(vbCFFiles) Then
    ' The clipboard holds a file list (e.g. from Explorer copy)
End If

GetText

Reads text data from the clipboard. Returns a String; returns an empty string if the clipboard does not currently hold data in the requested format.

Syntax: object.GetText( [ Format ] )

Format
optional A member of ClipboardConstants selecting which text format to retrieve: vbCFText (default), vbCFUnicodeText, vbCFRTF, or vbCFLink.
Dim s As String
s = Clipboard.GetText()                  ' plain text
Dim rtf As String
rtf = Clipboard.GetText(vbCFRTF)         ' RTF, if available

SetData

Places picture data onto the clipboard.

Syntax: object.SetData Picture [, Format ]

Picture
required A stdole.StdPicture carrying the picture to copy.
Format
optional A member of ClipboardConstants — which picture format to publish. When omitted, the format is inferred from the picture’s underlying type.

Note

Reserved for compatibility with VB6; not currently implemented in twinBASIC.

SetText

Places text data onto the clipboard. Note that SetText does not implicitly clear the clipboard first — call Clear explicitly when you want only this one value on the clipboard, so that no stale data of other formats survives.

Syntax: object.SetText Str [, Format ]

Str
required The String to publish.
Format
optional A member of ClipboardConstantsvbCFText (default), vbCFUnicodeText, vbCFRTF, or vbCFLink.
Clipboard.Clear
Clipboard.SetText "Plain text"
Clipboard.SetText "{\rtf1 \b Bold \b0 plain.}", vbCFRTF   ' add an RTF alternative