Rewriting Clipboard Code
Visual Basic 6 has a Clipboard object that allows you to store and retrieve text and pictures to and from the Windows Clipboard. In Visual Basic .NET, you manipulate the Clipboard using the System.Windows.Forms.Clipboard namespace. The new Clipboard classes are more flexible than those in Visual Basic 6, in that they allow you to set and retrieve data in a particular format and query the contents of the Clipboard to see what formats it supports. Unfortunately, Clipboard code cannot be upgraded automatically. However, you ll find it straightforward to implement the same functionality in Visual Basic .NET.
Let s look at an example. Suppose you have a Visual Basic 6 form with two PictureBox controls on it, sourcePictureBox and destinationPictureBox. The following code sets and retrieves the text VB Rocks and then copies a picture from one PictureBox to the Clipboard and into a second PictureBox:
'Set and retrieve text Dim s As String Clipboard.SetText "VB Rocks" s = Clipboard.GetText 'Set and retrieve a picture Clipboard.SetData Me.sourcePictureBox.Picture Me.destinationPictureBox.Picture = Clipboard.GetData
When this code is upgraded, the Clipboard code is left as is and is marked with upgrade warnings:
'UPGRADE_ISSUE: Clipboard method Clipboard.SetText was not upgraded.Clipboard.SetText("VB Rocks")
The upgraded code causes compile errors in Visual Basic .NET. To get the same functionality, we have to delete the upgraded code and replace it with code that uses the System.Windows.Forms.Clipboard objects:
'Set and retrieve text Dim s As String Dim stringData As IDataObject Dim getString As New DataObject(DataFormats.StringFormat) Clipboard.SetDataObject("VB Rocks", True) stringData = Clipboard.GetDataObject() If stringData.GetDataPresent(DataFormats.Text) Then s = stringData.GetData(DataFormats.Text, True) End If 'Set and retrieve a picture Dim pictureData As IDataObject Clipboard.SetDataObject(Me.sourcePictureBox.Image) pictureData = Clipboard.GetDataObject If pictureData.GetDataPresent(DataFormats.Bitmap) Then Me.destinationPictureBox.Image = _ pictureData.GetData(DataFormats.Bitmap, True) End If
The Visual Basic .NET code improves upon the Visual Basic 6 code by checking for the existence of a compatible Clipboard format before setting the value of the string and the PictureBox image.