Created: March 1, 1995
One of the nice features provided under Windows® 3.1 is its Clipboard. Data (text or graphics) can be copied from an application program to the Clipboard. This data can then be transferred from the Clipboard to a different application, to be processed in some way.
This article shows how you can copy selected items from a List Box control to the Clipboard. The Clipboard.GetText command copies information from the Clipboard to an object, such as a Text Box. Conversely, the Clipboard.SetText command copies information from an object to the Clipboard.
The following Visual Basic® application copies selected items from a List Box control to the Clipboard.
Sub Form_Load()
List1.AddItem "Vancouver, B.C."
List1.AddItem "Surrey, B.C."
List1.AddItem "White Rock, B.C."
List1.AddItem "Richmond, B.C."
End Sub
Sub Command1_Click()
Dim CopytoClip As String
Dim I As Integer
Clipboard.Clear
CopytoClip = ""
For I = 0 To List1.ListCount - 1
If List1.Selected(I) Then
CopytoClip = CopytoClip & Form1.List1.List(I) & Chr$(13) & Chr$(10)
End If
Next I
Clipboard.SetText CopytoClip
End Sub
Sub Command2_Click()
'display data stored in clipboard
Text1.Text = ""
Text1.Text = Clipboard.GetText(CF_TEXT)
End Sub
Sub Command3_Click()
End
End Sub
Const CF_TEXT = 1
After you execute this demonstration program, the List Box will contain the names of the four cities. Click one or more of these names to select them. Then, click the "Copy to Clipboard" command button. To verify that only the selected items were actually copied to the Clipboard, click the "Show Clipboard" command button. You can experiment with the program several times to verify that it works correctly. To terminate the application, click the Exit command button.