Clipboard.GetDataObject メソッド
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

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


クリップボードから返されるオブジェクトのデータ型はさまざまであるため、このメソッドはデータを IDataObject に格納して返します。そのため、IDataObject インターフェイスのメソッドを使用して、適切なデータ型のデータを抽出できます。
このメソッドは、100 ミリ秒間隔でデータの取得を 10 回試行します。試行がすべて失敗すると ExternalException をスローします。
![]() |
---|
Clipboard クラスは、シングル スレッド アパートメント (STA: Single Thread Apartment) モードに設定されているスレッドでだけ使用できます。このクラスを使用するには、Main メソッドが確実に STAThreadAttribute 属性でマークされているようにします。 |

Clipboard のメソッドを使用して、システム クリップボードにデータを貼り付けたり、システム クリップボードからデータを取得したりするコード例を次に示します。このコードは、button1、button2、textBox1、および textBox2 がフォーム上に配置されていることを前提にしています。
button1_Click メソッドは、SetDataObject を呼び出して、テキスト ボックス内で選択されているテキストを取り出し、システム クリップボードに貼り付けます。
button2_Click メソッドは、GetDataObject を呼び出して、システム クリップボードからデータを取得します。コードは、IDataObject と DataFormats を使用して、返されたデータを抽出します。抽出したデータは 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
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: 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.get_SelectedText().Equals("")) { Clipboard.SetDataObject(textBox1.get_SelectedText()); } else { textBox2.set_Text("No text selected in textBox1"); } } //button1_Click 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.set_Text((String)(iData.GetData(DataFormats.Text))); } else { // No it is not. textBox2.set_Text("Could not retrieve data off the clipboard."); } } //button2_Click
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 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からClipboard.GetDataObject メソッドを検索する場合は、下記のリンクをクリックしてください。

- Clipboard.GetDataObject メソッドのページへのリンク