Clipboard.GetDataObject Metoda

Definicja

Pobiera dane, które są obecnie w schowku systemowym.

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

Zwraca

IDataObject

Obiekt IDataObject , który reprezentuje dane aktualnie w Schowku lub null jeśli nie ma danych w Schowku.

Wyjątki

Nie można pobrać danych ze Schowka. Zwykle dzieje się tak, gdy Schowek jest używany przez inny proces.

Bieżący wątek nie jest w trybie apartamentu jednowątkowego (STA), a MessageLoop wartość właściwości to true. Dodaj element STAThreadAttribute do metody aplikacji Main .

Przykłady

Poniższy przykład kodu używa Clipboard metod do umieszczania danych i pobierania ich ze Schowka systemowego. W tym kodzie przyjęto założeniebutton1, że textBox1w formularzu umieszczono wartości , button2, i textBox2 .

Metoda button1_Click wywołuje metodę SetDataObject , aby pobrać zaznaczony tekst z pola tekstowego i umieścić go w schowku systemowym.

Metoda button2_Click wywołuje metodę GetDataObject w celu pobrania danych ze Schowka systemowego. Kod używa funkcji IDataObject i DataFormats do wyodrębniania zwróconych danych. Dane są wyświetlane w pliku 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

Uwagi

Ponieważ typ danych obiektu zwróconego ze Schowka może się różnić, ta metoda zwraca dane w IDataObjectobiekcie . Następnie możesz użyć metod interfejsu IDataObject , aby wyodrębnić dane w odpowiednim typie danych.

Ta metoda próbuje pobrać dane dziesięć razy w 100-milisekundowych interwałach i zgłasza błąd ExternalException , jeśli wszystkie próby nie powiedzie się.

Uwaga

Klasę Clipboard można używać tylko w wątkach ustawionych na tryb apartamentów pojedynczego wątku (STA). Aby użyć tej klasy, upewnij się, że metoda Main jest oznaczona atrybutem STAThreadAttribute .

Dotyczy

Zobacz też