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

要放置到剪貼簿上的資料。

例外狀況

資料無法放置到剪貼簿上。 這通常在剪貼簿由另一個處理序使用時發生。

目前執行緒 (Thread) 不是在單一執行緒 Apartment (STA) 模式。 將 STAThreadAttribute 加入至應用程式的 Main 方法。

data 的值是 null

範例

下列程式碼範例會使用 SetDataObject 將非持續性文字資料放在系統剪貼簿上。 在 方法中 button1_Click ,選取的文字會從 textBox1 剪貼簿複製並貼上。 在 方法中 button2_Click ,會從剪貼簿擷取資訊,並顯示在 中 textBox2 。 此程式碼假設 button1button2textBox1textBox2 已建立並放置在表單上。

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 毫秒間隔設定資料十次,如果所有嘗試都失敗,則會擲回 ExternalException

注意

物件必須可序列化,才能將它放在剪貼簿上。 如果您將不可序列化的物件傳遞至這個方法,它將會失敗,而不會擲回例外狀況。 如需序列化的詳細資訊,請參閱 System.Runtime.Serialization

類別 Clipboard 只能在設定為單一線程 Apartment (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

例外狀況

資料無法放置到剪貼簿上。 這通常在剪貼簿由另一個處理序使用時發生。

目前執行緒 (Thread) 不是在單一執行緒 Apartment (STA) 模式。 將 STAThreadAttribute 加入至應用程式的 Main 方法。

data 的值是 null

範例

下列方法會在應用程式中執行。 它會在系統剪貼簿的文字方塊中,放置所選文字資料的持續性複本。 此程式碼假設 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 。 此程式碼假設 button2textBox2 建立並放置在表單上。

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 毫秒間隔設定資料十次,如果所有嘗試都失敗,則會擲回 ExternalException

注意

物件必須可序列化,才能將它放在剪貼簿上。 如果您將不可序列化的物件傳遞至這個方法,它將會失敗,而不會擲回例外狀況。 如需序列化的詳細資訊,請參閱 System.Runtime.Serialization

類別 Clipboard 只能在設定為單一線程 Apartment (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

嘗試之間暫停的毫秒數。

例外狀況

目前執行緒 (Thread) 不是在單一執行緒 Apartment (STA) 模式。 將 STAThreadAttribute 加入至應用程式的 Main 方法。

datanull

retryTimes 小於零。

-或- retryDelay 小於零。

資料無法放置到剪貼簿上。 這通常在剪貼簿由另一個處理序使用時發生。

備註

如果剪貼簿忙於另一個執行緒或應用程式,將資料加入剪貼簿偶爾可能會失敗。 這個方法有助於在使用大量剪貼簿的環境中解決此問題。

copy如果 參數為 false ,當應用程式結束時,資料將會從系統剪貼簿中刪除。

注意

物件必須可序列化,才能將它放在剪貼簿上。 如果您將不可序列化的物件傳遞至這個方法,它將會失敗,而不會擲回例外狀況。 如需序列化的詳細資訊,請參閱 System.Runtime.Serialization

類別 Clipboard 只能在設定為單一線程 Apartment (STA) 模式的執行緒中使用。 若要使用此類別,請確定您的 Main 方法已以 STAThreadAttribute 屬性標示。

另請參閱

適用於