Editar

Compartilhar via


Clipboard.SetData(String, Object) Method

Definition

Clears the Clipboard and then adds data in the specified format.

public:
 static void SetData(System::String ^ format, System::Object ^ data);
public static void SetData (string format, object data);
static member SetData : string * obj -> unit
Public Shared Sub SetData (format As String, data As Object)

Parameters

format
String

The format of the data to set. See DataFormats for predefined formats.

data
Object

An Object representing the data to add.

Exceptions

The Clipboard could not be cleared. This typically occurs when the Clipboard is being used by another process.

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

data is null.

Examples

The following example demonstrates this member.

// Demonstrates SetData, ContainsData, and GetData.
public Object SwapClipboardFormattedData(String format, Object data)
{
    Object returnObject = null;
    if (Clipboard.ContainsData(format))
    {
        returnObject = Clipboard.GetData(format);
        Clipboard.SetData(format, data);
    }
    return returnObject;
}
' Demonstrates SetData, ContainsData, and GetData.
Public Function SwapClipboardFormattedData( _
    ByVal format As String, ByVal data As Object) As Object

    Dim returnObject As Object = Nothing

    If (Clipboard.ContainsData(format)) Then
        returnObject = Clipboard.GetData(format)
        Clipboard.SetData(format, data)
    End If

    Return returnObject

End Function

Remarks

If you do not know the format of the target application, you can store data in multiple formats using this method.

Data stored using this method can be converted to a compatible format when it is retrieved.

To retrieve data from the Clipboard in a particular format, first use the ContainsData method to determine whether the Clipboard contains data in that format before retrieving it with the GetData method.

Note

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.

Applies to

See also