Storing data to and reading from the Clipboard (Visual Basic)

The Clipboard can be used to store data, such as text and images. Because the Clipboard is shared by all active processes, it can be used to transfer data between them. The My.Computer.Clipboard object allows you to easily access the Clipboard and to read from and write to it.

Use the GetText method to read the text in the Clipboard. The following code reads the text and displays it in a message box. There must be text stored on the Clipboard for the example to run correctly.

MsgBox(My.Computer.Clipboard.GetText())


This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in Windows Forms Applications > Clipboard. For more information, see Code Snippets.

Use the GetImage method to retrieve an image from the Clipboard. This example checks to see if there is an image on the Clipboard before retrieving it and assigning it to PictureBox1.

If My.Computer.Clipboard.ContainsImage() Then
Dim grabpicture As System.Drawing.Image
grabpicture = My.Computer.Clipboard.GetImage()
picturebox1.Image = grabpicture
End If


This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in Windows Forms Applications > Clipboard.For more information, see Code Snippets.

Items placed on the Clipboard will persist even after the application is shut down.

Determining the type of file stored in the Clipboard

Data on the Clipboard may take a number of different forms, such as text, an audio file, or an image. In order to determine what sort of file is on the Clipboard, you can use methods such as ContainsAudio, ContainsFileDropList, ContainsImage, and ContainsText. The ContainsData method can be used if you have a custom format that you want to check.

Use the ContainsImage function to determine whether the data contained on the Clipboard is an image. The following code checks to see whether the data is an image and reports accordingly.

If My.Computer.Clipboard.ContainsImage() Then
MsgBox("Clipboard contains an image.")
Else
MsgBox("Clipboard does not contain an image.")
End If


Clearing the Clipboard

The Clear method clears the Clipboard. Because the Clipboard is shared by other processes, clearing it may have an impact on those processes.

The following code shows how to use the Clear method.

My.Computer.Clipboard.Clear()


Writing to the Clipboard

Use the SetText method to write text to the Clipboard. The following code writes the string "This is a test string" to the Clipboard.

My.Computer.Clipboard.SetText("This is a test string.")


The SetText method can accept a format parameter that contains a type of TextDataFormat. The following code writes the string "This is a test string" to the Clipboard as RTF text.

My.Computer.Clipboard.SetText("This is a test string.",
System.Windows.Forms.TextDataFormat.Rtf)


Use the SetData method to write data to the Clipboard. This example writes the DataObject dataChunk to the Clipboard in the custom format specialFormat.

My.Computer.Clipboard.SetData("specialFormat", dataChunk)


Use the SetAudio method to write audio data to the Clipboard. This example creates the byte array musicReader, reads the file cool.wav into it, and then writes it to the Clipboard.

Dim musicReader = My.Computer.FileSystem.ReadAllBytes("cool.wav")