Clipboard.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 。クリップボードにデータがない場合は null

例外

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

現在のスレッドがシングル スレッド アパートメント (STA) モードではなく、 MessageLoop プロパティ値が true。 アプリケーションの Main メソッドにSTAThreadAttributeを追加します。

次のコード例では、 Clipboard メソッドを使用してデータを配置し、システム クリップボードから取得します。 このコードは、 button1button2textBox1、および textBox2 がフォームに配置されていることを前提としています。

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

button2_Click メソッドはGetDataObjectを呼び出して、システム クリップボードからデータを取得します。 このコードでは、 IDataObjectDataFormats を使用して、返されたデータを抽出します。 データは 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 回試行し、すべての試行が失敗した場合に ExternalException をスローします。

Note

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

適用対象

こちらもご覧ください