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.
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.
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.
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.
Use the SetText method to write text to the Clipboard. The following code writes the string "This is a test string" to the Clipboard.
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.
Use the SetData method to write data to the Clipboard. This example writes the DataObject dataChunk to the Clipboard in the custom format specialFormat.
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.
Because the Clipboard can be accessed by other users, do not use it to store sensitive information, such as passwords or confidential data.