Clipboard.GetDataObject Yöntem

Tanım

Şu anda sistem Panosu'nda bulunan verileri alır.

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

Döndürülenler

IDataObject

IDataObject Şu anda Pano'da bulunan veya Pano'da null veri olmayan verileri temsil eden bir.

Özel durumlar

Veriler Pano'dan alınamadı. Bu durum genellikle Pano başka bir işlem tarafından kullanıldığında oluşur.

Geçerli iş parçacığı tek iş parçacıklı daire (STA) modunda değil ve MessageLoop özellik değeri şeklindedir true. öğesini STAThreadAttribute uygulamanızın yöntemine Main ekleyin.

Örnekler

Aşağıdaki kod örneği, verileri üzerine yerleştirmek ve sistem Panosu'ndan almak için yöntemleri kullanır Clipboard . Bu kod, , button2, textBox1ve textBox2 değerlerinin forma yerleştirildiğini varsayarbutton1.

yöntemi, button1_Click seçili metni metin kutusundan alıp sistem Panosu'na yerleştirmeyi çağırır SetDataObject .

button2_Click yöntemi, sistem Panosu'ndan veri almak için öğesini çağırırGetDataObject. Kod, döndürülen verileri ayıklamak için ve DataFormats kullanırIDataObject. Veriler içinde textBox2görüntülenir.

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

Açıklamalar

Pano'dan döndürülen nesnenin veri türü farklılık gösterebileceğinden, bu yöntem içindeki IDataObjectverileri döndürür. Ardından, uygun veri türündeki IDataObject verileri ayıklamak için arabiriminin yöntemlerini kullanabilirsiniz.

Bu yöntem, verileri 100 milisaniyelik aralıklarla on kez almayı dener ve tüm girişimler başarısız olursa bir ExternalException oluşturur.

Not

Clipboard sınıfı yalnızca tek iş parçacığı dairesi (STA) moduna ayarlanmış iş parçacıklarında kullanılabilir. Bu sınıfı kullanmak için yönteminizin Main özniteliğiyle işaretlendiğinden STAThreadAttribute emin olun.

Şunlara uygulanır

Ayrıca bkz.