Clipboard.SetDataObject 方法

定義

在系統剪貼簿上儲存指定的資料物件。

多載

SetDataObject(Object)

將指定的非持續性資料物件放在系統剪貼簿上。

SetDataObject(Object, Boolean)

將指定的資料物件放在系統剪貼簿上,並接受布林參數以指出在應用程式結束時是否應保留剪貼簿上的資料物件。

SetDataObject(Object)

將指定的非持續性資料物件放在系統剪貼簿上。

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)

參數

data
Object

要放在系統剪貼簿上的資料物件 (可實作 IDataObject 的物件)。

屬性

例外狀況

datanull

存取剪貼簿時發生錯誤。 例外狀況詳細資料將包括識別特定錯誤的 HResult,請參閱 ErrorCode

備註

根據預設,當應用程式結束時,系統剪貼簿 SetDataObject 上的資料會自動從剪貼簿清除。

注意

清除應用程式結束時剪貼簿的預設行為可能會與其他實作不同,這可能會在應用程式結束時保留剪貼簿上的資料,而不是預設清除它。 SetDataObject使用 多載並指定 copy 參數,就像不想在應用程式結束時從剪貼簿清除資料一樣 true

DataObject 提供 介面的基本實作 IDataObject

另請參閱

適用於

SetDataObject(Object, Boolean)

將指定的資料物件放在系統剪貼簿上,並接受布林參數以指出在應用程式結束時是否應保留剪貼簿上的資料物件。

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)

參數

data
Object

要放在系統剪貼簿上的資料物件 (可實作 IDataObject 的物件)。

copy
Boolean

true 可在應用程式結束時保留系統剪貼簿上的資料,false 則會在應用程式結束時清除系統剪貼簿中的資料。

屬性

例外狀況

datanull

存取剪貼簿時發生錯誤。 例外狀況詳細資料將包括識別特定錯誤的 HResult,請參閱 ErrorCode

範例

下列範例示範如何使用這個方法。


               // 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 提供 介面的基本實作 IDataObjectIsCurrent 會決定上一 SetDataObject 次呼叫之前放在剪貼簿上的資料物件。

另請參閱

適用於