Share via


How to: Write to 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 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 SetAudio, SetData, SetFileDropDownList, SetImage, and SetText methods allow you to place data on the Clipboard.

Security noteSecurity Note

Because the Clipboard can be accessed by other users, do not use it to store sensitive information, such as passwords or confidential data.

To write text to the Clipboard

  • Use the My.Computer.Clipboard.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.")
    

To write text to the Clipboard in a specific format

  • Use the My.Computer.Clipboard.SetText method to write text to the Clipboard, including the 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)
    

To write data to the Clipboard

  • Use the My.Computer.Clipboard.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)
    

See Also

Tasks

How to: Read from the Clipboard in Visual Basic

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

Reference

ClipboardProxy

TextDataFormat

SetText

SetData

SetDataObject