Name

Clipboard.GetDataObject Method

Class

System.Windows.Forms.Clipboard

Syntax

Clipboard.GetDataObject(  )

Return value

An IDataObject object that represents the data currently on the clipboard

Description

Retrieves data from the Clipboard

Rules at a Glance

  • If the Clipboard contains no data, the GetDataObject method returns Nothing.

  • Once you have an IDataObject object, you can use the members of the IDataObject class to get information about the Clipboard data, as shown in the following example. The relevant IDataObject members for Clipboard manipulation in VB are GetData, GetDataPresent, and GetFormats.

Example

The following example extracts the text that is currently on the Clipboard:

' Declare IDataObject variable and get clipboard IDataObject
Dim di As IDataObject = Clipboard.GetDataObject

Dim obj As Object

' Fire GetData method of IDataObject object to get clipboard data
obj = di.GetData(DataFormats.Text, False)

' Show the text, if any
If obj Is Nothing Then
   MsgBox("No text on clipboard.")
Else
   MsgBox(CStr(obj))
End If

VB.NET/VB 6 Differences

While the .NET Base Class Library uses the GetDataObject method to retrieve all data from the Clipboard, the Clipboard object in VB 6 included the GetFormat, GetData, and GetText methods to retrieve Clipboard data.

Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.