Export (0) Print
Expand All

Clipboard.SetDataObject Method (Object, Boolean)

Clears the Clipboard and then places data on it and specifies whether the data should remain after the application exits.

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

public static void SetDataObject(
	Object data,
	bool copy
)

Parameters

data
Type: System.Object

The data to place on the Clipboard.

copy
Type: System.Boolean

true if you want data to remain on the Clipboard after this application exits; otherwise, false.

ExceptionCondition
ExternalException

Data could not be placed on the Clipboard. This typically occurs when the Clipboard is being used by another process.

ThreadStateException

The current thread is not in single-threaded apartment (STA) mode. Add the STAThreadAttribute to your application's Main method.

ArgumentNullException

The value of data is null.

If the copy parameter is false, the data will be deleted from system Clipboard when the application exits.

This method attempts to set the data ten times in 100-millisecond intervals, and throws an ExternalException if all attempts are unsuccessful.

NoteNote

An object must be serializable for it to be put on the Clipboard. If you pass a non-serializable object to this method, it will fail without throwing an exception. See System.Runtime.Serialization for more information on serialization.

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.

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.

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

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.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft