Export (0) Print
Expand All

Clipboard.SetDataObject Method

Places data on the system clipboard.

Overload List

Places nonpersistent data on the system clipboard.

[Visual Basic] Overloads Public Shared Sub SetDataObject(Object)
[C#] public static void SetDataObject(object);
[C++] public: static void SetDataObject(Object*);
[JScript] public static function SetDataObject(Object);

Places data on the system clipboard and specifies whether the data should remain on the clipboard after the application exits.

[Visual Basic] Overloads Public Shared Sub SetDataObject(Object, Boolean)
[C#] public static void SetDataObject(object, bool);
[C++] public: static void SetDataObject(Object*, bool);
[JScript] public static function SetDataObject(Object, Boolean);

Example

The following method is run in an application. It places a persistent copy of the selected text data in the text box on the system clipboard. This code assumes button1, textBox1, and textBox2 have been created and placed on a form.

[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, True)
    Else
        textBox2.Text = "No text selected in textBox1"
    End If
End Sub 'button1_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, true);
    else
       textBox2.Text = "No text selected in textBox1";
 }

[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, true);
    else
       textBox2->Text = S"No text selected in textBox1";
 }

[JScript] 
private function button1_Click(sender : Object, e : System.EventArgs) {
    //Take the selected text from a text from a text box and put it on the clipboard.
    if(textBox1.SelectedText != "")
       Clipboard.SetDataObject(textBox1.SelectedText, true);
    else
       textBox2.Text = "No text selected in textBox1";
 }

In a different application, the following method retrieves the text from the system clipboard and pastes the text into textBox2. This code assumes button2 and textBox2 have been created and placed on a form.

[Visual Basic] 
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 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 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 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.";
    }
 }

See Also

Clipboard Class | Clipboard Members | System.Windows.Forms Namespace

Show:
© 2014 Microsoft