Provides static (in Visual Basic Shared) methods to place data on and retrieve data from the system Clipboard. This class cannot be inherited.
Object
Clipboard
[Visual Basic] NotInheritable Public Class Clipboard [C#] public sealed class Clipboard [C++] public __gc __sealed class Clipboard [JScript] public class Clipboard
For a list of predefined Clipboard formats, see the DataFormats class.
Call SetDataObject to put data on the Clipboard. To place a persistent copy of the data on the Clipboard, set the copy parameter to true.
Tip Place data on the Clipboard in multiple formats to maximize the possibility that a target application, whose format requirements you may not know, can successfully retrieve the data.
Call GetDataObject to retrieve data from the Clipboard. The data is returned as an object that implements the IDataObject interface. Use the methods specified by IDataObject and fields in DataFormats to extract the data from the object. If you do not know the format of the data you retrieved, call the GetFormats method of the IDataObject interface to get a list of all formats that data is stored in. Then call the GetData method of the IDataObject interface, and specify a format that your application can use.
Note All Windows applications share the system Clipboard, so the contents are subject to change when you switch to another application.
Namespace: System.WinForms
Assembly: System.WinForms.dll
The following example uses Clipboard methods to place data on and retrieve it from the system clipboard. This code assumes Button1, Button2, TextBox1, and TextBox2 have been placed on the form.
The Button1_Click method calls SetDataObject to take selected text from the text box and place it on the system clipboard.
The Button2_Click method calls GetDataObject to retrieve data from the system clipboard. The code uses IDataObject and DataFormats to extract the data returned and displays the data in TextBox2.
[Visual Basic]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Take the selected text from a text box and put it on the clipboard. Clipboard.SetDataObject(TextBox1.SelectedText) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Declare an IDataObject to hold the data returned from the clipboard. 'Then retrieve the data from the clipboard. Dim iData As IDataObject iData = Clipboard.GetDataObject 'Determine 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 = iData.GetData(DataFormats.Text).ToString Else 'No it is not. TextBox2.Text = "Could not retrieve data off the clipboard." End If End Sub
Clipboard Members | System.WinForms Namespace | DataObject | DataFormats | IDataObject