Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Clipboard Class

Provides methods to place data on and retrieve data from the system clipboard. This class cannot be inherited.

For a list of all members of this type, see Clipboard Members.

System.Object
   System.Windows.Forms.Clipboard

[Visual Basic]
NotInheritable Public Class Clipboard
[C#]
public sealed class Clipboard
[C++]
public __gc __sealed class Clipboard
[JScript]
public class Clipboard

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

For a list of predefined formats to use with the Clipboard class, see the DataFormats class.

Call SetDataObject to put data on the clipboard. To place a persistent copy of the data on the clipboard, set the copy parameter to true.

Note   Place data on the clipboard in multiple formats to maximize the possibility that a target application, whose format requirements you might not know, can successfully retrieve the data.

Call GetDataObject to retrieve data from the clipboard. The data is returned as an object that implements the IDataObject interface. Use the methods specified by IDataObject and fields in DataFormats to extract the data from the object. If you do not know the format of the data you retrieved, call the GetFormats method of the IDataObject interface to get a list of all formats that data is stored in. Then call the GetData method of the IDataObject interface, and specify a format that your application can use.

Note   All Windows applications share the system clipboard, so the contents are subject to change when you switch to another application.
Note   A class must be serializable for it to be put on the clipboard. See Serializing Objects for more information on serialization.

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 created and 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 and displays the data 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

Namespace: System.Windows.Forms

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

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

See Also

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.