Clipboard.SetDataObject 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
清除剪贴板然后,然后在其中添加数据。
重载
| 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 -> unitPublic Shared Sub SetDataObject (data As Object)参数
- data
- Object
要置于剪贴板中的数据。
例外
未能将数据置于剪贴板中。 这种情况通常发生在剪贴板正在被其他进程使用的时候。
当前线程未处于单线程单元 (STA) 模式下。 请将 STAThreadAttribute 添加到应用程序的 Main 方法中。
data 的值为 null。
示例
下面的代码示例用于 SetDataObject 将非持久性文本数据放置在系统剪贴板上。 在方法中 button1_Click ,所选文本从剪贴板复制 textBox1 并粘贴。 在方法中 button2_Click ,将从剪贴板检索信息并显示在剪贴板中 textBox2。 此代码假定button1已button2``textBox1``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) 模式的线程中使用。 若要使用此类,请确保使用属性标记STAThreadAttribute方法Main。
另请参阅
适用于
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 -> unitPublic Shared Sub SetDataObject (data As Object, copy As Boolean)参数
- data
- Object
要置于剪贴板中的数据。
- copy
- Boolean
如果想在退出应用程序后将数据保留在剪贴板中,则为 true;否则为 false。
例外
未能将数据置于剪贴板中。 这种情况通常发生在剪贴板正在被其他进程使用的时候。
当前线程未处于单线程单元 (STA) 模式下。 请将 STAThreadAttribute 添加到应用程序的 Main 方法中。
data 的值为 null。
示例
以下方法在应用程序中运行。 它将所选文本数据的持久副本放置在系统剪贴板上的文本框中。 此代码假定button1已textBox1``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) 模式的线程中使用。 若要使用此类,请确保使用属性标记STAThreadAttribute方法Main。
另请参阅
适用于
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 -> unitPublic 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 方法中。
data 为 null。
未能将数据置于剪贴板中。 这种情况通常发生在剪贴板正在被其他进程使用的时候。
注解
如果剪贴板正忙于另一个线程或应用程序,则向剪贴板添加数据有时会失败。 此方法可用于在使用大量剪贴板的环境中解决此问题。
如果参数 copy 为 false,则应用程序退出时,数据将从系统剪贴板中删除。
备注
对象必须可序列化,才能将其放在剪贴板上。 如果将不可序列化的对象传递给此方法,它将失败,而不会引发异常。 有关详细信息,请参阅 System.Runtime.Serialization 序列化。
类 Clipboard 只能在设置为单线程单元 (STA) 模式的线程中使用。 若要使用此类,请确保使用属性标记STAThreadAttribute方法Main。