Clipboard.GetDataObject Metodo

Definizione

Recupera i dati attualmente presenti negli Appunti di sistema.

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

Valori restituiti

Oggetto IDataObject che rappresenta i dati attualmente negli Appunti o null se non sono presenti dati negli Appunti.

Eccezioni

Impossibile recuperare i dati dagli Appunti. Ciò si verifica in genere quando gli Appunti vengono usati da un altro processo.

Il thread corrente non è in modalità apartment a thread singolo (STA) e il valore della MessageLoop proprietà è true. Aggiungere l'oggetto STAThreadAttribute al metodo dell'applicazione Main .

Esempio

Nell'esempio di codice seguente vengono usati metodi Clipboard per inserire e recuperare i dati dagli Appunti di sistema. Questo codice presuppone che , button2, textBox1e textBox2 siano stati inseriti button1nel form.

Il button1_Click metodo chiama SetDataObject per accettare testo selezionato dalla casella di testo e inserirlo negli Appunti di sistema.

Il button2_Click metodo chiama GetDataObject per recuperare i dati dagli Appunti di sistema. Il codice usa IDataObject e DataFormats per estrarre i dati restituiti. I dati vengono visualizzati in 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

Commenti

Poiché il tipo di dati dell'oggetto restituito dagli Appunti può variare, questo metodo restituisce i dati in un oggetto IDataObject. È quindi possibile usare i metodi dell'interfaccia IDataObject per estrarre i dati nel tipo di dati appropriato.

Questo metodo tenta di ottenere i dati dieci volte in intervalli di 100 millisecondi e genera un'eccezione ExternalException se tutti i tentativi hanno esito negativo.

Note

La classe Clipboard può essere utilizzata solo nei thread impostati sulla modalità STA (Single Thread Apartment), a thread singolo. Per usare questa classe, assicurarsi che il metodo Main sia contrassegnato con l'attributo STAThreadAttribute.

Si applica a

Vedi anche