Szerkesztés

Megosztás a következőn keresztül:


Clipboard.SetDataObject Method

Definition

Clears the Clipboard and then adds data to it.

Overloads

SetDataObject(Object)

Clears the Clipboard and then places nonpersistent data on it.

SetDataObject(Object, Boolean)

Clears the Clipboard and then places data on it and specifies whether the data should remain after the application exits.

SetDataObject(Object, Boolean, Int32, Int32)

Clears the Clipboard and then attempts to place data on it the specified number of times and with the specified delay between attempts, optionally leaving the data on the Clipboard after the application exits.

SetDataObject(Object)

Source:
Clipboard.cs
Source:
Clipboard.cs
Source:
Clipboard.cs

Clears the Clipboard and then places nonpersistent data on it.

public:
 static void SetDataObject(System::Object ^ data);
public static void SetDataObject (object data);
static member SetDataObject : obj -> unit
Public Shared Sub SetDataObject (data As Object)

Parameters

data
Object

The data to place on the Clipboard.

Exceptions

Data could not be placed on the Clipboard. This typically occurs when the Clipboard is being used by another process.

The current thread is not in single-threaded apartment (STA) mode. Add the STAThreadAttribute to your application's Main method.

The value of data is null.

Examples

The following code example uses SetDataObject to place nonpersistent text data onto the system Clipboard. In the button1_Click method, the selected text is copied from textBox1 and pasted on the Clipboard. In the button2_Click method, the information is retrieved from the Clipboard and displayed in textBox2. This code assumes button1, button2, textBox1, and textBox2 have been created and placed on a form.

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

Remarks

Data will be deleted from system Clipboard when the application exits.

This method attempts to set the data ten times in 100-millisecond intervals, and throws an ExternalException if all attempts are unsuccessful.

Note

An object must be serializable for it to be put on the Clipboard. If you pass a non-serializable object to this method, it will fail without throwing an exception. See System.Runtime.Serialization for more information on serialization.

The Clipboard class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.

See also

Applies to

SetDataObject(Object, Boolean)

Source:
Clipboard.cs
Source:
Clipboard.cs
Source:
Clipboard.cs

Clears the Clipboard and then places data on it and specifies whether the data should remain after the application exits.

public:
 static void SetDataObject(System::Object ^ data, bool copy);
public static void SetDataObject (object data, bool copy);
static member SetDataObject : obj * bool -> unit
Public Shared Sub SetDataObject (data As Object, copy As Boolean)

Parameters

data
Object

The data to place on the Clipboard.

copy
Boolean

true if you want data to remain on the Clipboard after this application exits; otherwise, false.

Exceptions

Data could not be placed on the Clipboard. This typically occurs when the Clipboard is being used by another process.

The current thread is not in single-threaded apartment (STA) mode. Add the STAThreadAttribute to your application's Main method.

The value of data is null.

Examples

The following method is run in an application. It places a persistent copy of the selected text data in the text box on the system Clipboard. This code assumes button1, textBox1, and textBox2 have been created and placed on a form.

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, true );
      }
      else
      {
         textBox2->Text = "No text selected in textBox1";
      }
   }
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, true);
    else
       textBox2.Text = "No text selected in textBox1";
 }
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, True)
    Else
        textBox2.Text = "No text selected in textBox1"
    End If
End Sub

In a different application, the following method retrieves the text from the system Clipboard and pastes the text into textBox2. This code assumes button2 and textBox2 have been created and placed on a form.

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 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 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

Remarks

If the copy parameter is false, the data will be deleted from system Clipboard when the application exits.

This method attempts to set the data ten times in 100-millisecond intervals, and throws an ExternalException if all attempts are unsuccessful.

Note

An object must be serializable for it to be put on the Clipboard. If you pass a non-serializable object to this method, it will fail without throwing an exception. See System.Runtime.Serialization for more information on serialization.

The Clipboard class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.

See also

Applies to

SetDataObject(Object, Boolean, Int32, Int32)

Source:
Clipboard.cs
Source:
Clipboard.cs
Source:
Clipboard.cs

Clears the Clipboard and then attempts to place data on it the specified number of times and with the specified delay between attempts, optionally leaving the data on the Clipboard after the application exits.

public:
 static void SetDataObject(System::Object ^ data, bool copy, int retryTimes, int retryDelay);
public static void SetDataObject (object data, bool copy, int retryTimes, int retryDelay);
static member SetDataObject : obj * bool * int * int -> unit
Public Shared Sub SetDataObject (data As Object, copy As Boolean, retryTimes As Integer, retryDelay As Integer)

Parameters

data
Object

The data to place on the Clipboard.

copy
Boolean

true if you want data to remain on the Clipboard after this application exits; otherwise, false.

retryTimes
Int32

The number of times to attempt placing the data on the Clipboard.

retryDelay
Int32

The number of milliseconds to pause between attempts.

Exceptions

The current thread is not in single-threaded apartment (STA) mode. Add the STAThreadAttribute to your application's Main method.

data is null.

retryTimes is less than zero.

-or-

retryDelay is less than zero.

Data could not be placed on the Clipboard. This typically occurs when the Clipboard is being used by another process.

Remarks

Adding data to the Clipboard can occasionally fail if the Clipboard is busy with another thread or application. This method is useful to work around this issue in environments with heavy Clipboard use.

If the copy parameter is false, the data will be deleted from system Clipboard when the application exits.

Note

An object must be serializable for it to be put on the Clipboard. If you pass a non-serializable object to this method, it will fail without throwing an exception. See System.Runtime.Serialization for more information on serialization.

The Clipboard class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.

See also

Applies to