语言

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的对象)。

特性

例外

data 是 null。

访问剪贴板时出错。 异常详细信息将包括标识特定错误的一个 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 如果应用程序退出,则从系统剪贴板中清除数据。

特性

例外

data 是 null。

访问剪贴板时出错。 异常详细信息将包括标识特定错误的一个 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 提供接口的基本实现 IDataObject 。 IsCurrent 确定上次 SetDataObject 调用之前放置在剪贴板上的数据对象。

另请参阅

适用于