Skip to content

Clipboard class

The Clipboard class wraps the system clipboard --- the Win32 inter-application copy-and-paste API --- 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:

vb
' 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:

ConstantValueMeaning
vbCFText1ANSI plain text.
vbCFBitmap2DDB (device-dependent bitmap).
vbCFMetafile3Windows metafile (WMF).
vbCFDIB8DIB (device-independent bitmap).
vbCFPalette9Colour palette.
vbCFUnicodeText13UTF-16 plain text.
vbCFEMetafile14Enhanced metafile (EMF).
vbCFFiles15A list of file paths (CF_HDROP).
vbCFLink&HFFFFBF00DDE link (legacy OLE-1 link source).
vbCFRTF&HFFFFBF01Rich 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 handle pictures, restricted to the bitmap and metafile formats.

Picture data

The picture methods --- GetData and SetData --- are declared but not yet connected.

INFO

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.

INFO

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

INFO

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 to ensure that no stale data of other formats survives alongside the new value.

Syntax: object.SetText Str [, Format ]

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

twinBASIC and LOGO copyright of "WaynePhillipsEA" author