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.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public sealed class Clipboard
public final class Clipboard
public final class Clipboard
Not applicable.

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.

NoteNote:

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.

In Microsoft .NET Framework version 2.0, the Clipboard class provides additional methods that make it easier to work with the system Clipboard. Call the Clear method to remove all data from the Clipboard. To add data of a particular format to the Clipboard, call the appropriate SetFormat method, such as SetText, or call the SetData method to specify the format. To retrieve data of a particular format from the Clipboard, first call the appropriate ContainsFormat method (such as ContainsText) method to determine whether the Clipboard contains data in that format, and then call the appropriate GetFormat method (such as GetText) to retrieve the data if the Clipboard contains it. To specify the format in these operations, call the ContainsData and GetData methods instead.

NoteNote:

All Windows-based applications share the system Clipboard, so the contents are subject to change when you switch to another application.

An object must be serializable for it to be put on the Clipboard. If you pass a non-serializable object to a Clipboard method, the method will fail without throwing an exception. See Serialization for more information on serialization. If your target application requires a very specific data format, the headers added to the data in the serialization process may prevent the application from recognizing your data. To preserve your data format, add your data as a Byte array to a MemoryStream and pass the MemoryStream to the SetData method.

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.

Special considerations may be necessary when using the metafile format with the Clipboard. Due to a limitation in the current implementation of the DataObject class, the metafile format used by the .NET Framework may not be recognized by applications that use an older metafile format. In this case, you must interoperate with the Win32 Clipboard application programming interfaces (APIs). For more information, see article 323530, "Metafiles on Clipboard Are Not Visible to All Applications," in the Microsoft Knowledge Base at http://support.microsoft.com.

Windows Mobile 2003 for Pocket PC, Windows Mobile 2003 for Smartphone, Windows CE Platform Note: In Windows CE, a memory stream pasted from the Clipboard can have a slightly larger size than the memory stream copied to the Clipboard, because extra bytes can be appended to the end of the original memory stream. To accurately retrieve the memory stream either prefix the object with its size to determine how to receive it, or copy a DataObject to the Clipboard containing the memory stream and a string value of its size.

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

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.";
    }
 }
 

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.get_SelectedText().Equals("")) {
        Clipboard.SetDataObject(textBox1.get_SelectedText());
    }
    else {
        textBox2.set_Text("No text selected in textBox1");
    }
} //button1_Click

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.set_Text((String)(iData.GetData(DataFormats.Text)));
    }
    else {
        // No it is not.
        textBox2.set_Text("Could not retrieve data off the clipboard.");
    }
} //button2_Click

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.";
    }
 }
 

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.