Visual Basic Concepts
Working with Multiple Formats on the Clipboard
You can actually place several pieces of data on the Clipboard at the same time, as long as each piece is in a different format. This is useful because you don’t know what application will be pasting the data, so supplying the data in several different formats enhances the chance that you will provide it in a format that the other application can use. The other Clipboard methods — GetData, SetData, and GetFormat — allow you to deal with data formats other than text by supplying a number that specifies the format. These formats are described in the following table, along with the corresponding number.
Constant | Description |
vbCFLink | Dynamic data exchange link. |
vbCFText | Text. Examples earlier in this chapter all use this format. |
vbCFBitmap | Bitmap. |
vbCFMetafile | Metafile. |
vbCFDIB | Device-independent bitmap. |
vbCFPalette | Color palette. |
You can use the last four formats when cutting and pasting data from picture box controls. The following code provides generalized Cut, Copy, and Paste commands that work with any of the standard controls.
Private Sub mnuCopy_Click ()
Clipboard.Clear
If TypeOf Screen.ActiveControl Is TextBox Then
Clipboard.SetText Screen.ActiveControl.SelText
ElseIf TypeOf Screen.ActiveControl Is ComboBox Then
Clipboard.SetText Screen.ActiveControl.Text
ElseIf TypeOf Screen.ActiveControl Is PictureBox _
Then
Clipboard.SetData Screen.ActiveControl.Picture
ElseIf TypeOf Screen.ActiveControl Is ListBox Then
Clipboard.SetText Screen.ActiveControl.Text
Else
' No action makes sense for the other controls.
End If
End Sub
Private Sub mnuCut_Click ()
' First do the same as a copy.
mnuCopy_Click
' Now clear contents of active control.
If TypeOf Screen.ActiveControl Is TextBox Then
Screen.ActiveControl.SelText = ""
ElseIf TypeOf Screen.ActiveControl Is ComboBox Then
Screen.ActiveControl.Text = ""
ElseIf TypeOf Screen.ActiveControl Is PictureBox _
Then
Screen.ActiveControl.Picture = LoadPicture()
ElseIf TypeOf Screen.ActiveControl Is ListBox Then
Screen.ActiveControl.RemoveItem Screen.ActiveControl.ListIndex
Else
' No action makes sense for the other controls.
End If
End Sub
Private Sub mnuPaste_Click ()
If TypeOf Screen.ActiveControl Is TextBox Then
Screen.ActiveControl.SelText = Clipboard.GetText()
ElseIf TypeOf Screen.ActiveControl Is ComboBox Then
Screen.ActiveControl.Text = Clipboard.GetText()
ElseIf TypeOf Screen.ActiveControl Is PictureBox _
Then
Screen.ActiveControl.Picture = _
Clipboard.GetData()
ElseIf TypeOf Screen.ActiveControl Is ListBox Then
Screen.ActiveControl.AddItem Clipboard.GetText()
Else
' No action makes sense for the other controls.
End If
End Sub