Retrieves the data that is currently on the system Clipboard.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Data could not be retrieved from the Clipboard. This typically occurs when the Clipboard is being used by another process.
Because the data type of the object returned from the Clipboard can vary, this method returns the data in an IDataObject. Then you can use methods of the IDataObject interface to extract the data in its proper data type.
This method attempts to get the data ten times in 100-millisecond intervals, and throws an ExternalException if all attempts are unsuccessful.
The following code example uses Clipboard methods to place data on and retrieve it from the system Clipboard. This code assumes button1, button2, textBox1, and textBox2 have been placed on the form.
The button1_Click method calls SetDataObject to take selected text from the text box and place it on the system Clipboard.
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 Clipboard.SetDataObject(textBox1.SelectedText) Else 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) Else ' No it is not. textBox2.Text = "Could not retrieve data off the clipboard." End If End Sub 'button2_Click