(Print this page)

.NET Clipboard object
Published date: Friday, August 1, 2003
On: Moer and Éric Moreau's web site

The Clipboard is one of the features that existed in VB6 that got completely revamped in .Net. Even the tool that upgrades VB6 code to VB.Net code does not deal with the VB6 Clipboard object. If you try to upgrade such a project, you get "upgrade issues".

The .Net Clipboard class is hosted by the System.Windows.Forms namespace. It is not a complex object. It only has two public shared (meaning that you don't need to create an instance of the Clipboard object to use them) methods: SetDataObject and GetDataObject. One important thing to know is that this class cannot be inherited. Although the class has a simple interface, you can implement wonderful features into your own application with it.

Copying data to the Clipboard

To put data on the Clipboard, you use the SetDataObject method that has two overloads: 

  • Overloads Public Shared Sub SetDataObject(Object)
  • Overloads Public Shared Sub SetDataObject(Object, Boolean)

Set the Boolean parameter to true when you want your data to remain on the clipboard after the application terminates (the default is False).

For example, to copy the content of a textbox (only the selected part if any) to the clipboard, you use this syntax:

'Copy the selected text to the clipboard
Dim strX As String = TextBox1.SelectedText
'if no text is selected, copy the entire text
If strX.Length = 0 Then strX = TextBox1.Text
'Copy data if any to copy
If strX.Length > 0 Then
    Clipboard.SetDataObject(strX)
End If

Retrieving data from the Clipboard

To retrieve data from the clipboard, you need to use the GetDataObject method. You also need to specify the format you want to retrieve because the clipboard can stock more then one information at any time (but only one of each format). More then 20 formats exist like Text, HTML, CSV, Wave audio, Bitmap, RTF, …

For example, to retrieve the text from the previous code sample into a different textbox, you could use this:

'Retrieves the data from the clipboard
Dim iData As IDataObject = Clipboard.GetDataObject

'Check to see if Text data is present
If iData.GetDataPresent(DataFormats.Text) Then
    TextBox2.Text = CType(iData.GetData(DataFormats.Text), String)
Else
    TextBox2.Text = "No data!"
End If

This code can also be used to retrieve data copied to the clipboard from another source like Word.

Copying a file to the clipboard

The Clipboard object is normally able to detect the proper format of the data you copy to it but sometimes you may need to force a specific format. Let's say you want to copy a file to the clipboard, not the filename, the complete file. To do it, you need to use a DataObject instance like this:

Dim DataObject As New DataObject
Dim file(0) As String
file(0) = "c:\temp.txt"
DataObject.SetData(DataFormats.FileDrop, file)
Clipboard.SetDataObject(DataObject, True)
Try it by creating a temp.txt file at the root of your C drive (or change the filename) and running this code and then open Windows Explorer and paste into a different folder. The complete file will be copied.

Copying data to Excel

Still using the Clipboard object, you may easily copy data to Excel. You simply need to create a string in which columns are delimited by tab and rows by carriage returns. This is the code you need:

'Copy a string to the clipboard.
Dim sData As String
sData = "FirstName" & vbTab & "LastName" & vbTab & "Birthdate" & vbCr _
    & "Bill" & vbTab & "Brown" & vbTab & "2/5/85" & vbCr _
    & "Joe" & vbTab & "Thomas" & vbTab & "1/1/91"
Clipboard.SetDataObject(sData)

'Create a new workbook in Excel.
Dim oExcel As Object
Dim oBook As Object
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add

'Paste the data.
oBook.Worksheets(1).Range("A1").Select()
oBook.Worksheets(1).Paste()

Conclusion

Powerful and easy. That's how I describe this object!

There are many other things you can do by using the techniques above. Take some time to download my demo available with this article to find how to: 

  • Save a graphic that is loaded in the Clipboard as a bitmap file. 
  • Copy a class instance to the Clipboard (the class must be serializable – see my column in the July edition). 
  • Add spelling and grammar checking of Word.

I hope you appreciated the topic and see you next month.


(Print this page)