Share via


Clipboard.GetDataObject Método

Definición

Recupera los datos que se encuentran actualmente en el Portapapeles del sistema.

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

Devoluciones

IDataObject

IDataObject que representa los datos que existen actualmente en el Portapapeles, o null si no hay datos en el Portapapeles.

Excepciones

No se han podido recuperar datos del Portapapeles. Esto ocurre normalmente cuando otro proceso utiliza el Portapapeles.

El subproceso actual no está en modo de contenedor uniproceso (STA) y el valor de la propiedad MessageLoop es true. Agregue STAThreadAttribute al método Main de la aplicación.

Ejemplos

En el ejemplo de código siguiente se usan Clipboard métodos para colocar datos en el Portapapeles del sistema y recuperarlos. Este código supone que button1, button2``textBox1, y textBox2 se han colocado en el formulario.

El button1_Click método llama SetDataObject a para tomar el texto seleccionado del cuadro de texto y colocarlo en el Portapapeles del sistema.

El button2_Click método llama GetDataObject a para recuperar datos del Portapapeles del sistema. El código usa IDataObject y DataFormats para extraer los datos devueltos. Los datos se muestran en 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

Comentarios

Dado que el tipo de datos del objeto devuelto desde el Portapapeles puede variar, este método devuelve los datos de .IDataObject A continuación, puede usar métodos de la IDataObject interfaz para extraer los datos en su tipo de datos adecuado.

Este método intenta obtener los datos diez veces en intervalos de 100 milisegundos y produce un ExternalException si todos los intentos no se realizan correctamente.

Nota

La Clipboard clase solo se puede usar en subprocesos establecidos en modo de apartamento de subproceso único (STA). Para usar esta clase, asegúrese de que Main el método está marcado con el STAThreadAttribute atributo .

Se aplica a

Consulte también