Clipboard.SetDataObject 方法

定義

在系統剪貼簿上儲存指定的資料物件。Stores a specified data object on the system Clipboard.

多載

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

參數

data
Object

要放在系統剪貼簿上的資料物件 (可實作 IDataObject 的物件)。A data object (an object that implements IDataObject) to place on the system Clipboard.

屬性

例外狀況

datanulldata is null.

存取剪貼簿時發生錯誤。An error occurred when accessing the Clipboard. 例外狀況詳細資料將包括識別特定錯誤的 HResult,請參閱 ErrorCodeThe exception details will include an HResult that identifies the specific error; see ErrorCode.

備註

根據預設,當應用程式結束時,會自動從剪貼簿中清除使用 SetDataObject 放在系統剪貼簿中的資料。By default, data placed on the system Clipboard with SetDataObject is automatically cleared from the Clipboard when the application exits.

注意

在應用程式結束時清除剪貼簿的預設行為可能與其他執行方式不同,這可能會讓剪貼簿上的資料在應用程式結束時,而不是預設清除。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. 如果您不想要在應用程式結束時從剪貼簿清除資料,請使用 SetDataObject 多載,並將 copy 參數指定為 trueUse the SetDataObject overload and specify the copy parameter as true if you do not want data cleared from the Clipboard on application exit.

DataObject 提供 IDataObject 介面的基本實作為。DataObject provides a basic implementation of the IDataObject interface.

另請參閱

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

參數

data
Object

要放在系統剪貼簿上的資料物件 (可實作 IDataObject 的物件)。A data object (an object that implements IDataObject) to place on the system Clipboard.

copy
Boolean

true 可在應用程式結束時保留系統剪貼簿上的資料,false 則會在應用程式結束時清除系統剪貼簿中的資料。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.

屬性

例外狀況

datanulldata is null.

存取剪貼簿時發生錯誤。An error occurred when accessing the Clipboard. 例外狀況詳細資料將包括識別特定錯誤的 HResult,請參閱 ErrorCodeThe exception details will include an HResult that identifies the specific error; see ErrorCode.

範例

下列範例示範如何使用這個方法。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)

備註

DataObject 提供 IDataObject 介面的基本實作為。DataObject provides a basic implementation of the IDataObject interface. IsCurrent 會決定前一個 SetDataObject 呼叫之前放在剪貼簿上的資料物件。IsCurrent determines the data object previously placed on the clipboard by the last SetDataObject call.

另請參閱

適用於