How to: Read from the Clipboard in 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 and write to it. The GetText, GetImage, GetData, GetAudioStream, and GetFileDropDownList methods allow you to specify what type of data you would like to read from the Clipboard.

If data cannot be retrieved from the Clipboard, an ExternalException is thrown.

To read text from the Clipboard and display it

  • Use the My.Computer.Clipboard.GetText method to read the text. 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 How to: Insert Snippets Into Your Code (Visual Basic).

To read an image from the Clipboard

  • Use the My.Computer.Clipboard.GetImage method to read an image from the Clipboard. The following code reads an image from the Clipboard and assigns it to the Image property of Button1. There must be an image stored on the Clipboard and a Button named Button1 for the example to run correctly.

    Button1.Image = My.Computer.Clipboard.GetImage()
    

    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 How to: Insert Snippets Into Your Code (Visual Basic).

See Also

Tasks

How to: Write to the Clipboard in Visual Basic

How to: Determine What Type of File is Stored on the Clipboard in Visual Basic

Reference

My.Computer.Clipboard Object Members

My.Computer.Clipboard Object