Using the clipboard is very similar to using drag and drop. To save a single piece of data, call the Clipboard object’s SetDataObject method, passing it the data that you want to save. For example, the following code copies the text in the txtLastName control to the clipboard:
Clipboard.SetDataObject(txtLastName.Text)
Copying data to the clipboard in multiple formats is very similar to dragging and dropping multiple data formats. First, create a DataObject and use its SetData method to store the data exactly as before. Then call the Clipboard object’s SetDataObject method, passing it the DataObject.
The following code adds Rtf, Text, and Html data to the clipboard:
' Copy data to the clipboard. Private Sub btnCopy_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnCopy.Click ' Make a DataObject. Dim data_object As New DataObject ' Add the data in various formats. data_object.SetData(DataFormats.Rtf, rchSource.Rtf) data_object.SetData(DataFormats.Text, rchSource.Text) ' Build the HTML version. Dim html_text As String html_text = "<HTML>" & vbCrLf html_text &= " <HEAD>The Quick Brown Fox</HEAD>" & vbCrLf html_text &= " <BODY>" & vbCrLf html_text &= rchSource.Text & vbCrLf html_text &= " </BODY>" & vbCrLf & "</HTML>" data_object.SetData(DataFormats.Html, html_text) ' Copy data to the clipboard. Clipboard.SetDataObject(data_object) End Sub
To retrieve data from the clipboard, use the GetDataObject method to get an IDataObject representing the data. Use that object’s GetDataPresent method to see if a data type is present, and use its GetData method to get data with a particular format.
The following code displays Rtf, Text, and Html data from the clipboard:
' Paste data from the clipboard. Private Sub btnPaste_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnPaste.Click Dim data_object As IDataObject = Clipboard.GetDataObject() If data_object.GetDataPresent(DataFormats.Rtf) Then rchTarget.Rtf = data_object.GetData(DataFormats.Rtf).ToString lblRtf.Text = data_object.GetData(DataFormats.Rtf).ToString Else rchTarget.Text = "" lblRtf.Text = "" End If If data_object.GetDataPresent(DataFormats.Text) Then lblTarget.Text = data_object.GetData(DataFormats.Text).ToString Else lblTarget.Text = "" End If If data_object.GetDataPresent(DataFormats.Html) Then lblHtml.Text = data_object.GetData(DataFormats.Html).ToString Else lblHtml.Text = "" End If End Sub
The IDataObject returned by the GetDataObject method also provides a GetFormats method that returns an array of the data formats available. This array is very similar to the one returned by the GetFormats method provided by the DragEnter event described earlier in this chapter.
You can copy and paste objects using the clipboard much as you drag and drop objects. Simply make the object’s class serializable and add an instance of the class to the DataObject.
The following code shows how a program can copy and paste an Employee object. The btnCopy_Click event handler makes an Employee object and a DataObject. It passes the Employee object to the DataObject object’s SetData method, giving it the data format name Employee. The program then passes the DataObject to the Clipboard object’s SetDataObject method. The btnPaste_Click event handler retrieves the clipboard’s data object and uses its GetDataPresent method to see if the clipboard is holding data with the Employee format. If the data is present, the program uses the data object’s GetData method to fetch the data, casts it into an Employee object, and displays the object’s property values.
' Copy the Employee to the clipboard. Private Sub btnCopy_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnCopy.Click Dim emp As New Employee(txtFirstName.Text, txtLastName.Text) Dim data_object As New DataObject data_object.SetData("Employee", emp) Clipboard.SetDataObject(data_object) End Sub ' Paste data from the clipboard. Private Sub btnPaste_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnPaste.Click Dim data_object As IDataObject = Clipboard.GetDataObject() If data_object.GetDataPresent("Employee") Then Dim emp As Employee = _ DirectCast(data_object.GetData("Employee"), Employee) txtPasteFirstName.Text = emp.FirstName txtPasteLastName.Text = emp.LastName End If End Sub
The following table lists most of the methods provided by the Clipboard object, including several that make working with common data types easier.
Method | Purpose |
---|---|
Clear | Removes all data from the clipboard. |
ContainsAudio | Returns True if the clipboard contains audio data. |
ContainsData | Returns True if the clipboard contains data in a particular format. |
ContainsFileDropList | Returns True if the clipboard contains a file drop list. |
ContainsImage | Returns True if the clipboard contains an image. |
ContainsText | Returns True if the clipboard contains text. |
GetAudioStream | Returns the audio stream contained in the clipboard. |
GetData | Returns data in a specific format. |
GetDataObject | Returns the clipboard’s DataObject. |
GetFileDropList | Returns the file drop list contained in the clipboard. |
GetImage | Returns the image contained in the clipboard. |
GetText | Returns the text contained in the clipboard. |
SetAudio | Saves audio bytes or an audio stream in the clipboard. |
SetData | Saves data in a particular format in the clipboard. |
SetDataObject | Saves the data defined by a DataObject in the clipboard. |
SetFileDropList | Saves a file drop list in the clipboard. The data should be a StringCollection containing the file names. |
SetImage | Saves an image in the clipboard. |
SetText | Saves text in the clipboard. |
The following code retrieves file drop list data from the clipboard:
Private Sub btnPaste_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnPaste.Click lstFiles.Items.Clear() If Clipboard.ContainsFileDropList() Then Dim file_names As StringCollection = Clipboard.GetFileDropList() For Each file_name As String In file_names lstFiles.Items.Add(file_name) Next file_name End If End Sub