संपादित करें

इसके माध्यम से साझा किया गया


Clipboard.SetDataObject Method

Definition

Stores a specified data object on the system Clipboard.

Overloads

SetDataObject(Object)

Places a specified non-persistent data object on the system Clipboard.

SetDataObject(Object, Boolean)

Places a specified data object on the system Clipboard and accepts a Boolean parameter that indicates whether the data object should be left on the Clipboard when the application exits.

SetDataObject(Object)

Places a specified non-persistent data object on the system Clipboard.

public:
 static void SetDataObject(System::Object ^ data);
[System.Security.SecurityCritical]
public static void SetDataObject (object data);
public static void SetDataObject (object data);
[<System.Security.SecurityCritical>]
static member SetDataObject : obj -> unit
static member SetDataObject : obj -> unit
Public Shared Sub SetDataObject (data As Object)

Parameters

data
Object

A data object (an object that implements IDataObject) to place on the system Clipboard.

Attributes

Exceptions

data is null.

An error occurred when accessing the Clipboard. The exception details will include an HResult that identifies the specific error; see ErrorCode.

Remarks

By default, data placed on the system Clipboard with SetDataObject is automatically cleared from the Clipboard when the application exits.

Note

The default behavior of clearing the Clipboard on application exit might differ from other implementations, which might leave data on the Clipboard on application exit rather than clearing it by default. Use the SetDataObject overload and specify the copy parameter as true if you do not want data cleared from the Clipboard on application exit.

DataObject provides a basic implementation of the IDataObject interface.

See also

Applies to

SetDataObject(Object, Boolean)

Places a specified data object on the system Clipboard and accepts a Boolean parameter that indicates whether the data object should be left on the Clipboard when the application exits.

public:
 static void SetDataObject(System::Object ^ data, bool copy);
[System.Security.SecurityCritical]
public static void SetDataObject (object data, bool copy);
public static void SetDataObject (object data, bool copy);
[<System.Security.SecurityCritical>]
static member SetDataObject : obj * bool -> unit
static member SetDataObject : obj * bool -> unit
Public Shared Sub SetDataObject (data As Object, copy As Boolean)

Parameters

data
Object

A data object (an object that implements IDataObject) to place on the system Clipboard.

copy
Boolean

true to leave the data on the system Clipboard when the application exits; false to clear the data from the system Clipboard when the application exits.

Attributes

Exceptions

data is null.

An error occurred when accessing the Clipboard. The exception details will include an HResult that identifies the specific error; see ErrorCode.

Examples

The following example demonstrates the use of this method.


               // For this example, the data to be placed on the clipboard is a simple
               // string.
               string textData = "I want to put this string on the clipboard.";
               // The example will enable auto-conversion of data for this data object.
               bool autoConvert = true;

               // Create a new data object, specifying the data format, data to encapsulate, and enabling
               // auto-conversion services.
               DataObject data = new DataObject(DataFormats.UnicodeText, (Object)textData, autoConvert);
               
               // If the data to be copied is supposed to be persisted after the application ends, 
               // then set the second parameter of SetDataObject to true.
               if(persistentData)
               {
                   // Place the persisted data on the clipboard.
                   Clipboard.SetDataObject(data, true);
               }
               else
               {
                   // Place the non-persisted data on the clipboard.
                   Clipboard.SetDataObject(data, false);
               }

               // If you keep a copy of the source data object, you can use the IsCurrent method to see if
               // the data object is still on the clipboard.
               bool isOriginalDataObject = Clipboard.IsCurrent(data);

' For this example, the data to be placed on the clipboard is a simple
' string.
Dim textData As String = "I want to put this string on the clipboard."
' The example will enable auto-conversion of data for this data object.
Dim autoConvert As Boolean = True

' Create a new data object, specifying the data format, data to encapsulate, and enabling
' auto-conversion services.
Dim data As New DataObject(DataFormats.UnicodeText, CType(textData, Object), autoConvert)

' If the data to be copied is supposed to be persisted after the application ends, 
' then set the second parameter of SetDataObject to true.
If persistentData Then
    ' Place the persisted data on the clipboard.
    Clipboard.SetDataObject(data, True)
Else
    ' Place the non-persisted data on the clipboard.
    Clipboard.SetDataObject(data, False)
End If

' If you keep a copy of the source data object, you can use the IsCurrent method to see if
' the data object is still on the clipboard.
Dim isOriginalDataObject As Boolean = Clipboard.IsCurrent(data)

Remarks

DataObject provides a basic implementation of the IDataObject interface. IsCurrent determines the data object previously placed on the clipboard by the last SetDataObject call.

See also

Applies to