Clipboard.SetDataObject メソッド

定義

クリップボードをクリアし、データを追加します。

オーバーロード

名前 説明
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に表示されます。 このコードでは、 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 をスローします。

Note

オブジェクトをクリップボードに配置するには、オブジェクトをシリアル化できる必要があります。 シリアル化できないオブジェクトをこのメソッドに渡すと、例外がスローされずに失敗します。 シリアル化の詳細については、 System.Runtime.Serialization を参照してください。

Clipboard クラスは、単一スレッド アパートメント (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

次のメソッドは、アプリケーションで実行されます。 選択したテキスト データの永続的なコピーがシステム クリップボードのテキスト ボックスに配置されます。 このコードは、 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 ミリ秒間隔で 10 回試行し、すべての試行が失敗した場合に ExternalException をスローします。

Note

オブジェクトをクリップボードに配置するには、オブジェクトをシリアル化できる必要があります。 シリアル化できないオブジェクトをこのメソッドに渡すと、例外がスローされずに失敗します。 シリアル化の詳細については、 System.Runtime.Serialization を参照してください。

Clipboard クラスは、単一スレッド アパートメント (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を追加します。

datanullです。

retryTimes が 0 未満です。

-又は-

retryDelay が 0 未満です。

クリップボードにデータを配置できませんでした。 これは通常、クリップボードが別のプロセスで使用されている場合に発生します。

注釈

クリップボードが別のスレッドまたはアプリケーションでビジー状態の場合、クリップボードへのデータの追加が失敗することがあります。 この方法は、クリップボードの使用が多い環境でこの問題を回避するのに役立ちます。

copy パラメーターがfalseされている場合、アプリケーションの終了時にシステム クリップボードからデータが削除されます。

Note

オブジェクトをクリップボードに配置するには、オブジェクトをシリアル化できる必要があります。 シリアル化できないオブジェクトをこのメソッドに渡すと、例外がスローされずに失敗します。 シリアル化の詳細については、 System.Runtime.Serialization を参照してください。

Clipboard クラスは、単一スレッド アパートメント (STA) モードに設定されたスレッドでのみ使用できます。 このクラスを使用するには、 Main メソッドが STAThreadAttribute 属性でマークされていることを確認します。

こちらもご覧ください

適用対象