Clipboard.SetDataObject 方法

定义

清除剪贴板,然后将数据添加到剪贴板。

重载

名称 说明
SetDataObject(Object)

清除剪贴板,然后将非持久性数据置于其中。

SetDataObject(Object, Boolean)

清除剪贴板,然后将数据放在其中,并指定数据在应用程序退出后是否应保留。

SetDataObject(Object, Boolean, Int32, Int32)

清除剪贴板,然后尝试将数据放置在指定的次数以及尝试之间的指定延迟,可以选择在应用程序退出后将数据保留在剪贴板上。

SetDataObject(Object)

清除剪贴板,然后将非持久性数据置于其中。

public:
 static void SetDataObject(System::Object ^ data);
public static void SetDataObject(object data);
static member SetDataObject : obj -> unit
Public Shared Sub SetDataObject (data As Object)

参数

data
Object

要放置在剪贴板上的数据。

例外

无法在剪贴板上放置数据。 当剪贴板被另一个进程使用时,通常会发生这种情况。

当前线程不在单线程单元(STA)模式下。 将它 STAThreadAttribute 添加到应用程序 Main 的方法。

值为 datanull.

示例

下面的代码示例用于 SetDataObject 将非持久性文本数据放置在系统剪贴板上。 在方法中 button1_Click ,所选文本将从剪贴板上 textBox1 复制并粘贴。 在 button2_Click 方法中,将从剪贴板检索信息并显示在剪贴板中 textBox2。 此代码假定button1button2textBox1创建并textBox2放置在窗体上。

private:
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Takes the selected text from a text box and puts it on the clipboard.
      if ( !textBox1->SelectedText->Equals( "" ) )
      {
         Clipboard::SetDataObject( textBox1->SelectedText );
      }
      else
      {
         textBox2->Text = "No text selected in textBox1";
      }
   }

   void button2_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Declares an IDataObject to hold the data returned from the clipboard.
      // Retrieves the data from the clipboard.
      IDataObject^ iData = Clipboard::GetDataObject();
      
      // Determines whether the data is in a format you can use.
      if ( iData->GetDataPresent( DataFormats::Text ) )
      {
         // Yes it is, so display it in a text box.
         textBox2->Text = (String^)(iData->GetData( DataFormats::Text ));
      }
      else
      {
         // No it is not.
         textBox2->Text = "Could not retrieve data off the clipboard.";
      }
   }
private void button1_Click(object sender, System.EventArgs e) {
    // Takes the selected text from a text box and puts it on the clipboard.
    if(textBox1.SelectedText != "")
       Clipboard.SetDataObject(textBox1.SelectedText);
    else
       textBox2.Text = "No text selected in textBox1";
 }
 
 private void button2_Click(object sender, System.EventArgs e) {
    // Declares an IDataObject to hold the data returned from the clipboard.
    // Retrieves the data from the clipboard.
    IDataObject iData = Clipboard.GetDataObject();
 
    // Determines whether the data is in a format you can use.
    if(iData.GetDataPresent(DataFormats.Text)) {
       // Yes it is, so display it in a text box.
       textBox2.Text = (String)iData.GetData(DataFormats.Text); 
    }
    else {
       // No it is not.
       textBox2.Text = "Could not retrieve data off the clipboard.";
    }
 }
Private Sub button1_Click(sender As Object, e As System.EventArgs)
    ' Takes the selected text from a text box and puts it on the clipboard.
    If textBox1.SelectedText <> "" Then
        Clipboard.SetDataObject(textBox1.SelectedText)
    Else
        textBox2.Text = "No text selected in textBox1"
    End If
End Sub
 
Private Sub button2_Click(sender As Object, e As System.EventArgs)
    ' Declares an IDataObject to hold the data returned from the clipboard.
    ' Retrieves the data from the clipboard.
    Dim iData As IDataObject = Clipboard.GetDataObject()
    
    ' Determines whether the data is in a format you can use.
    If iData.GetDataPresent(DataFormats.Text) Then
        ' Yes it is, so display it in a text box.
        textBox2.Text = CType(iData.GetData(DataFormats.Text), String)
    Else
        ' No it is not.
        textBox2.Text = "Could not retrieve data off the clipboard."
    End If
End Sub

注解

应用程序退出时,将从系统剪贴板中删除数据。

此方法尝试以 100 毫秒间隔设置数据 10 次,如果所有尝试均失败,则会引发一次 ExternalException

注释

对象必须可序列化,才能将其放在剪贴板上。 如果将不可序列化的对象传递给此方法,它将失败,而不会引发异常。 有关序列化的详细信息,请参阅 System.Runtime.Serialization

Clipboard 类只能在设置为单线程单元 (STA) 模式的线程中使用。 若要使用此类,请确保使用 Main 特性标记 STAThreadAttribute 方法。

另请参阅

适用于

SetDataObject(Object, Boolean)

清除剪贴板,然后将数据放在其中,并指定数据在应用程序退出后是否应保留。

public:
 static void SetDataObject(System::Object ^ data, bool copy);
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

要放置在剪贴板上的数据。

copy
Boolean

true 如果希望此应用程序退出后数据保留在剪贴板上;否则,为 false.

例外

无法在剪贴板上放置数据。 当剪贴板被另一个进程使用时,通常会发生这种情况。

当前线程不在单线程单元(STA)模式下。 将它 STAThreadAttribute 添加到应用程序 Main 的方法。

值为 datanull.

示例

以下方法在应用程序中运行。 它将所选文本数据的持久副本放置在系统剪贴板上的文本框中。 此代码假定 button1textBox1创建并 textBox2 放置在窗体上。

private:
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Takes the selected text from a text box and puts it on the clipboard.
      if ( !textBox1->SelectedText->Equals( "" ) )
      {
         Clipboard::SetDataObject( textBox1->SelectedText, true );
      }
      else
      {
         textBox2->Text = "No text selected in textBox1";
      }
   }
private void button1_Click(object sender, System.EventArgs e) {
    // Takes the selected text from a text box and puts it on the clipboard.
    if(textBox1.SelectedText != "")
       Clipboard.SetDataObject(textBox1.SelectedText, true);
    else
       textBox2.Text = "No text selected in textBox1";
 }
Private Sub button1_Click(sender As Object, e As System.EventArgs)
    ' Takes the selected text from a text box and puts it on the clipboard.
    If textBox1.SelectedText <> "" Then
        Clipboard.SetDataObject(textBox1.SelectedText, True)
    Else
        textBox2.Text = "No text selected in textBox1"
    End If
End Sub

在不同的应用程序中,以下方法从系统剪贴板检索文本,并将文本粘贴到 textBox2其中。 此代码假定 button2 并已创建并 textBox2 放置在窗体上。

private:
   void button2_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Declares an IDataObject to hold the data returned from the clipboard.
      // Retrieves the data from the clipboard.
      IDataObject^ iData = Clipboard::GetDataObject();
      
      // Determines whether the data is in a format you can use.
      if ( iData->GetDataPresent( DataFormats::Text ) )
      {
         
         // Yes it is, so display it in a text box.
         textBox2->Text = (String^)(iData->GetData( DataFormats::Text ));
      }
      else
      {
         
         // No it is not.
         textBox2->Text = "Could not retrieve data off the clipboard.";
      }
   }
private void button2_Click(object sender, System.EventArgs e) {
    // Declares an IDataObject to hold the data returned from the clipboard.
    // Retrieves the data from the clipboard.
    IDataObject iData = Clipboard.GetDataObject();
 
    // Determines whether the data is in a format you can use.
    if(iData.GetDataPresent(DataFormats.Text)) {
       // Yes it is, so display it in a text box.
       textBox2.Text = (String)iData.GetData(DataFormats.Text); 
    }
    else {
       // No it is not.
       textBox2.Text = "Could not retrieve data off the clipboard.";
    }
 }
Private Sub button2_Click(sender As Object, e As System.EventArgs)
    ' Declares an IDataObject to hold the data returned from the clipboard.
    ' Retrieves the data from the clipboard.
    Dim iData As IDataObject = Clipboard.GetDataObject()
    
    ' Determines whether the data is in a format you can use.
    If iData.GetDataPresent(DataFormats.Text) Then
        ' Yes it is, so display it in a text box.
        textBox2.Text = CType(iData.GetData(DataFormats.Text), String)
    Else
        ' No it is not.
        textBox2.Text = "Could not retrieve data off the clipboard."
    End If
End Sub

注解

copy如果参数为false,则应用程序退出时,数据将从系统剪贴板中删除。

此方法尝试以 100 毫秒间隔设置数据 10 次,如果所有尝试均失败,则会引发一次 ExternalException

注释

对象必须可序列化,才能将其放在剪贴板上。 如果将不可序列化的对象传递给此方法,它将失败,而不会引发异常。 有关序列化的详细信息,请参阅 System.Runtime.Serialization

Clipboard 类只能在设置为单线程单元 (STA) 模式的线程中使用。 若要使用此类,请确保使用 Main 特性标记 STAThreadAttribute 方法。

另请参阅

适用于

SetDataObject(Object, Boolean, Int32, Int32)

清除剪贴板,然后尝试将数据放置在指定的次数以及尝试之间的指定延迟,可以选择在应用程序退出后将数据保留在剪贴板上。

public:
 static void SetDataObject(System::Object ^ data, bool copy, int retryTimes, int retryDelay);
public static void SetDataObject(object data, bool copy, int retryTimes, int retryDelay);
static member SetDataObject : obj * bool * int * int -> unit
Public Shared Sub SetDataObject (data As Object, copy As Boolean, retryTimes As Integer, retryDelay As Integer)

参数

data
Object

要放置在剪贴板上的数据。

copy
Boolean

true 如果希望此应用程序退出后数据保留在剪贴板上;否则,为 false.

retryTimes
Int32

尝试将数据放置在剪贴板上的次数。

retryDelay
Int32

尝试之间暂停的毫秒数。

例外

当前线程不在单线程单元(STA)模式下。 将它 STAThreadAttribute 添加到应用程序 Main 的方法。

datanull

retryTimes 小于零。

-或-

retryDelay 小于零。

无法在剪贴板上放置数据。 当剪贴板被另一个进程使用时,通常会发生这种情况。

注解

如果剪贴板正忙于另一个线程或应用程序,则向剪贴板添加数据偶尔会失败。 此方法可用于在使用大量剪贴板的环境中解决此问题。

copy如果参数为false,则应用程序退出时,数据将从系统剪贴板中删除。

注释

对象必须可序列化,才能将其放在剪贴板上。 如果将不可序列化的对象传递给此方法,它将失败,而不会引发异常。 有关序列化的详细信息,请参阅 System.Runtime.Serialization

Clipboard 类只能在设置为单线程单元 (STA) 模式的线程中使用。 若要使用此类,请确保使用 Main 特性标记 STAThreadAttribute 方法。

另请参阅

适用于