語言

Clipboard.SetDataObject 方法

定義

清除剪貼簿,然後新增資料。

多載

名稱 Description
SetDataObject(Object)

清除剪貼簿,然後放置非持久的資料。

SetDataObject(Object, Boolean)

清除剪貼簿,然後將資料放入上,並指定應用程式結束後資料是否應該保留。

SetDataObject(Object, Boolean, Int32, Int32)

清除剪貼簿,然後嘗試在指定次數及間隔內放置資料,選擇性地在應用程式結束後將資料留在剪貼簿上。

SetDataObject(Object)

來源:
Clipboard.cs
來源:
Clipboard.cs
來源:
Clipboard.cs
來源:
Clipboard.cs
來源:
Clipboard.cs

清除剪貼簿,然後放置非持久的資料。

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加入你的申請方法。

的 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 毫秒為間隔設定資料十次,若所有嘗試皆未成功則拋出 。ExternalException

Note

物件必須可序列化,才能放在剪貼簿上。 如果你把不可序列化的物件傳給這個方法,它會失敗且不會拋出例外。 更多關於序號的資訊請參見 System.Runtime.Serialization 。

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

另請參閱

適用於

SetDataObject(Object, Boolean)

來源:
Clipboard.cs
來源:
Clipboard.cs
來源:
Clipboard.cs
來源:
Clipboard.cs
來源:
Clipboard.cs

清除剪貼簿,然後將資料放入上,並指定應用程式結束後資料是否應該保留。

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加入你的申請方法。

的 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 毫秒為間隔設定資料十次,若所有嘗試皆未成功則拋出 。ExternalException

Note

物件必須可序列化,才能放在剪貼簿上。 如果你把不可序列化的物件傳給這個方法,它會失敗且不會拋出例外。 更多關於序號的資訊請參見 System.Runtime.Serialization 。

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

另請參閱

適用於

SetDataObject(Object, Boolean, Int32, Int32)

來源:
Clipboard.cs
來源:
Clipboard.cs
來源:
Clipboard.cs
來源:
Clipboard.cs
來源:
Clipboard.cs

清除剪貼簿,然後嘗試在指定次數及間隔內放置資料,選擇性地在應用程式結束後將資料留在剪貼簿上。

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加入你的申請方法。

data 是 null。

retryTimes 小於零。

-或-

retryDelay 小於零。

資料無法放入剪貼簿。 這通常發生在夾板被其他程序使用時。

備註

如果夾板忙於其他執行緒或應用程式,加入資料有時會失敗。 這種方法對於在大量使用剪貼簿的環境中,能有效繞過這個問題。

若參數 copy 為 false,應用程式退出時資料會從系統剪貼簿中刪除。

Note

物件必須可序列化,才能放在剪貼簿上。 如果你把不可序列化的物件傳給這個方法,它會失敗且不會拋出例外。 更多關於序號的資訊請參見 System.Runtime.Serialization 。

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

另請參閱

適用於