Clipboard.SetDataObject Method (Object)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Clears the Clipboard and then places nonpersistent data on it.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Public Shared Sub SetDataObject (
	data As Object


Type: System.Object

The data to place on the Clipboard.

Exception Condition

Data could not be placed on the Clipboard. This typically occurs when the Clipboard is being used by another process.


The current thread is not in single-threaded apartment (STA) mode. Add the STAThreadAttribute to your application's Main method.


The value of data is null.

Data will be deleted from system Clipboard when the application exits.

This method attempts to set the data ten times in 100-millisecond intervals, and throws an ExternalException if all attempts are unsuccessful.


An object must be serializable for it to be put on the Clipboard. If you pass a non-serializable object to this method, it will fail without throwing an exception. See System.Runtime.Serialization for more information on serialization.

The Clipboard class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.

The following code example uses SetDataObject to place nonpersistent text data onto the system Clipboard. In the button1_Click method, the selected text is copied from textBox1 and pasted on the Clipboard. In the button2_Click method, the information is retrieved from the Clipboard and displayed in textBox2. This code assumes button1, button2, textBox1, and textBox2 have been created and placed on a form.

Private Sub button1_Click(sender As Object, e As System.EventArgs)
    ' Takes the selected text from a text box and puts it on the clipboard.
    If textBox1.SelectedText <> "" Then
        textBox2.Text = "No text selected in textBox1"
    End If
End Sub 'button1_Click

Private Sub button2_Click(sender As Object, e As System.EventArgs)
    ' Declares an IDataObject to hold the data returned from the clipboard.
    ' Retrieves the data from the clipboard.
    Dim iData As IDataObject = Clipboard.GetDataObject()

    ' Determines whether the data is in a format you can use.
    If iData.GetDataPresent(DataFormats.Text) Then
        ' Yes it is, so display it in a text box.
        textBox2.Text = CType(iData.GetData(DataFormats.Text), String)
        ' No it is not.
        textBox2.Text = "Could not retrieve data off the clipboard."
    End If
End Sub 'button2_Click


for adding data to the system Clipboard that is of type String or is in one of the following data formats: Text, UnicodeText, or CommaSeparatedValue. Associated enumeration: UIPermissionClipboard.OwnClipboard


for adding data of any type or format to the system Clipboard. Associated enumeration: UIPermissionClipboard.AllClipboard

.NET Framework
Available since 1.1
Return to top