Clipboard.GetDataObject メソッド

定義

現在システム クリップボードにあるデータを取得します。

public:
 static System::Windows::Forms::IDataObject ^ GetDataObject();
public static System.Windows.Forms.IDataObject GetDataObject ();
public static System.Windows.Forms.IDataObject? GetDataObject ();
static member GetDataObject : unit -> System.Windows.Forms.IDataObject
Public Shared Function GetDataObject () As IDataObject

戻り値

IDataObject

現在クリップボードに格納されているデータを表す IDataObject。データがクリップボードに格納されていない場合は null

例外

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

現在のスレッドがシングル スレッド アパートメント (STA: Single Thread Apartment) モードになっておらず、MessageLoop プロパティ値が true です。 アプリケーションの Main メソッドに STAThreadAttribute を追加してください。

次のコード例では、メソッドを使用 Clipboard してデータを配置し、システム クリップボードから取得します。 このコードは、, button2, をtextBox1想定button1し、textBox2フォーム上に配置されています。

このメソッドは button1_Click 、テキスト ボックスから選択したテキストを取得し、システム クリップボードに配置するために呼び出 SetDataObject します。

button2_Clickシステム クリップボードからデータを取得するメソッド呼び出しGetDataObject。 このコードでは、返されたデータを使用 IDataObject して DataFormats 抽出します。 にデータが表示されます 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

注釈

クリップボードから返されるオブジェクトのデータ型は異なる場合があるため、このメソッド IDataObjectは 、 その後、インターフェイスのメソッドを IDataObject 使用して、適切なデータ型のデータを抽出できます。

このメソッドは、100 ミリ秒間隔で 10 回データの取得を試み、すべての試行が失敗した場合に an ExternalException をスローします。

注意

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

適用対象

こちらもご覧ください