Clipboard.GetDataObject メソッド

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

Public Shared Function GetDataObject() As IDataObject
[C#]
public static IDataObject GetDataObject();
[C++]
public: static IDataObject* GetDataObject();
[JScript]
public static function GetDataObject() : IDataObject;

戻り値

現在クリップボードに格納されているデータを表す IDataObject 。データがクリップボードに格納されていない場合は null 参照 (Visual Basic では Nothing) 。

例外

例外の種類 条件
ExternalException クリップボードからデータを取得できませんでした。
ThreadStateException アプリケーションの ApartmentState プロパティは、 ApartmentState.STA には設定されません。

解説

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

使用例

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

button1_Click メソッドは、 SetDataObject を呼び出して、テキスト ボックス内で選択されているテキストを取り出し、システム クリップボードに貼り付けます。

button2_Click メソッドは、 GetDataObject を呼び出して、システム クリップボードからデータを取得します。コードは、 IDataObjectDataFormats を使用して、返されたデータを抽出します。抽出したデータは textBox2 に表示されます。

 
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 'button1_Click
 
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 'button2_Click

[C#] 
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.";
    }
 }
 

[C++] 
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(S""))
       Clipboard::SetDataObject(textBox1->SelectedText);
    else
       textBox2->Text = S"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 = dynamic_cast<String*>(iData->GetData(DataFormats::Text)); 
    }
    else {
       // No it is not.
       textBox2->Text = S"Could not retrieve data off the clipboard.";
    }
 }
 

[JScript] 
private function button1_Click(sender : Object, e : System.EventArgs) {
    //Take the selected text from a text box and put it on the clipboard.
    if(textBox1.SelectedText != "")
       Clipboard.SetDataObject(textBox1.SelectedText);
    else
       textBox2.Text = "No text selected in textBox1";
 }
 
 private function button2_Click(sender : Object, e : System.EventArgs) {
    //Declare an IDataObject to hold the data returned from the clipboard.
    //Then retrieve the data from the clipboard.
    var iData : IDataObject = Clipboard.GetDataObject();
 
    //Determine 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.";
    }
 }
 

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

Clipboard クラス | Clipboard メンバ | System.Windows.Forms 名前空間 | DataObject | DataFormats | SetDataObject | IDataObject