Visual Basic Concepts
Cutting, Copying, and Pasting Text with the Clipboard
Two of the most useful Clipboard methods are SetText and GetText. These two methods transfer string data to and from the Clipboard, as shown in Figure 12.2.
Figure 12.2 Moving data to and from the Clipboard with SetText and GetText
SetText copies text onto the Clipboard, replacing whatever text was stored there before. You use SetText like a statement. Its syntax is:
Clipboard.SetTextdata[, format]
GetText returns text stored on the Clipboard. You use it like a function:
destination = Clipboard.GetText()
By combining the SetText and GetText methods with the selection properties introduced in "Working with Selected Text," you can easily write Copy, Cut, and Paste commands for a text box. The following event procedures implement these commands for controls named mnuCopy, mnuCut, and mnuPaste:
Private Sub mnuCopy_Click ()
Clipboard.Clear
Clipboard.SetText Text1.SelText
End Sub
Private Sub mnuCut_Click ()
Clipboard.Clear
Clipboard.SetText Text1.SelText
Text1.SelText = ""
End Sub
Private Sub mnuPaste_Click ()
Text1.SelText = Clipboard.GetText()
End Sub
Note The example works best if these are menu controls, because you can use menus while Text1 has the focus.
Notice that both the Copy and Cut procedures first empty the Clipboard with the Clear method. (The Clipboard is not cleared automatically because you may want to place data on the Clipboard in several different formats, as described in "Working with Multiple Formats on the Clipboard" later in this chapter.) Both the Copy and Cut procedures then copy the selected text in Text1 onto the Clipboard with the following statement:
Clipboard.SetText Text1.SelText
In the Paste command, the GetText method returns the string of text currently on the Clipboard. An assignment statement then copies this string into the selected portion of the text box (Text1.SelText
). If no text is currently selected, Visual Basic places this text at the insertion point in the text box:
Text1.SelText = Clipboard.GetText()
This code assumes that all text is transferred to and from the text box Text1, but the user can copy, cut, and paste between Text1 and controls on other forms.
Because the Clipboard is shared by the entire environment, the user can also transfer text between Text1 and any application using the Clipboard.
Working with the ActiveControl Property
If you want the Copy, Cut, and Paste commands to work with any text box that has the focus, use the ActiveControl property of the Screen object. The following code provides a reference to whichever control has the focus:
Screen.ActiveControl
You can use this fragment just like any other reference to a control. If you know that the control is a text box, you can refer to any of the properties supported for text boxes, including Text, SelText, and SelLength. The following code assumes that the active control is a text box, and uses the SelText property:
Private Sub mnuCopy_Click ()
Clipboard.Clear
Clipboard.SetText Screen.ActiveControl.SelText
End Sub
Private Sub mnuCut_Click ()
Clipboard.Clear
Clipboard.SetText Screen.ActiveControl.SelText
Screen.ActiveControl.SelText = ""
End Sub
Private Sub mnuPaste_Click ()
Screen.ActiveControl.SelText = Clipboard.GetText()
End Sub