Clipboard.GetDataObject 方法

检索当前位于系统剪贴板中的数据。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Shared Function GetDataObject As IDataObject
用法
Dim returnValue As IDataObject

returnValue = Clipboard.GetDataObject
public static IDataObject GetDataObject ()
public:
static IDataObject^ GetDataObject ()
public static IDataObject GetDataObject ()
public static function GetDataObject () : IDataObject

返回值

IDataObject,表示系统剪贴板中当前的数据;如果剪贴板中没有数据,则为 空引用(在 Visual Basic 中为 Nothing)。

异常

异常类型 条件

ExternalException

从剪贴板中检索不到数据。这种情况通常发生在剪贴板正在被其他进程使用的时候。

ThreadStateException

当前线程未处于单线程单元 (STA) 模式下,且 Application.MessageLoop 属性值为 true。请将 STAThreadAttribute 添加到应用程序的 Main 方法中。

备注

因为从剪贴板返回的对象的数据类型可以不同,所以此方法返回 IDataObject 中的数据。然后,可以使用 IDataObject 接口的方法以正确的数据类型提取数据。

在获取数据时,此方法会尝试十次(每次间隔 100 毫秒),如果所有尝试均不成功,将引发 ExternalException

提示

Clipboard 类只能用于设置为单线程单元 (STA) 模式的线程中。若要使用此类,请确保已使用 STAThreadAttribute 属性标记 Main 方法。

示例

下面的代码示例使用 Clipboard 方法将数据置于系统剪贴板中,并从中检索这些数据。此代码假定 button1button2textBox1textBox2 已经置于窗体中。

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
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.";
    }
 }
 

.NET Framework 安全性

平台

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

Clipboard 类
Clipboard 成员
System.Windows.Forms 命名空间
DataObject
DataFormats
SetDataObject
IDataObject