Export (0) Print
Expand All

Clipboard.GetDataObject Method

Retrieves the data that is currently on the system clipboard.

[Visual Basic]
Public Shared Function GetDataObject() As IDataObject
[C#]
public static IDataObject GetDataObject();
[C++]
public: static IDataObject* GetDataObject();
[JScript]
public static function GetDataObject() : IDataObject;

Return Value

An IDataObject that represents the data currently on the clipboard, or a null reference (Nothing in Visual Basic) if there is no data on the clipboard.

Exceptions

Exception Type Condition
ExternalException Data could not be retrieved from the clipboard.
ThreadStateException The ApartmentState property of the application is not set to ApartmentState.STA.

Remarks

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.

Example

The following 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.

The button2_Click method calls GetDataObject to retrieve data from the system clipboard. The code uses IDataObject and DataFormats to extract the data returned. The data is displayed in textBox2.

[Visual Basic] 
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

[C#] 
private void button1_Click(object sender, System.EventArgs e) {
    // Takes the selected text from a text box and puts it on the clipboard.
    if(textBox1.SelectedText != "")
       Clipboard.SetDataObject(textBox1.SelectedText);
    else
       textBox2.Text = "No text selected in textBox1";
 }
 
 private void button2_Click(object sender, System.EventArgs e) {
    // Declares an IDataObject to hold the data returned from the clipboard.
    // Retrieves the data from the clipboard.
    IDataObject iData = Clipboard.GetDataObject();
 
    // Determines whether the data is in a format you can use.
    if(iData.GetDataPresent(DataFormats.Text)) {
       // Yes it is, so display it in a text box.
       textBox2.Text = (String)iData.GetData(DataFormats.Text); 
    }
    else {
       // No it is not.
       textBox2.Text = "Could not retrieve data off the clipboard.";
    }
 }
 

[C++] 
private:
 void button1_Click(Object* /*sender*/, System::EventArgs* /*e*/) {
    // Takes the selected text from a text box and puts it on the clipboard.
    if(!textBox1->SelectedText->Equals(S""))
       Clipboard::SetDataObject(textBox1->SelectedText);
    else
       textBox2->Text = S"No text selected in textBox1";
 }

 void button2_Click(Object* /*sender*/, System::EventArgs* /*e*/) {
    // Declares an IDataObject to hold the data returned from the clipboard.
    // Retrieves the data from the clipboard.
    IDataObject* iData = Clipboard::GetDataObject();
 
    // Determines whether the data is in a format you can use.
    if(iData->GetDataPresent(DataFormats::Text)) {
       // Yes it is, so display it in a text box.
       textBox2->Text = dynamic_cast<String*>(iData->GetData(DataFormats::Text)); 
    }
    else {
       // No it is not.
       textBox2->Text = S"Could not retrieve data off the clipboard.";
    }
 }
 

[JScript] 
private function button1_Click(sender : Object, e : System.EventArgs) {
    //Take the selected text from a text box and put it on the clipboard.
    if(textBox1.SelectedText != "")
       Clipboard.SetDataObject(textBox1.SelectedText);
    else
       textBox2.Text = "No text selected in textBox1";
 }
 
 private function button2_Click(sender : Object, e : System.EventArgs) {
    //Declare an IDataObject to hold the data returned from the clipboard.
    //Then retrieve the data from the clipboard.
    var iData : IDataObject = Clipboard.GetDataObject();
 
    //Determine whether the data is in a format you can use.
    if(iData.GetDataPresent(DataFormats.Text)) {
       //Yes it is, so display it in a text box.
       textBox2.Text = String(iData.GetData(DataFormats.Text)); 
    }
    else {
       //No it is not.
       textBox2.Text = "Could not retrieve data off the clipboard.";
    }
 }
 

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

Clipboard Class | Clipboard Members | System.Windows.Forms Namespace | DataObject | DataFormats | SetDataObject | IDataObject

Show:
© 2014 Microsoft