Copying data into the Clipboard

Copying a piece of information into the clipboard is as easy as calling the Clipboard.SetDataObject: you can pass this method a string, an image, and so on. If you pass it a string that contains text in Rich Text Format (RTF), the Clipboard object detects this format automatically. For example, the following procedure copies the selected portion of a TextBox control (or the entire control’s contents, if no text is selected) into the clipboard:

Sub CopyFromTextBox(ByVal tb As TextBox)    ' Copy the TextBox's selected text to the clipboard.    Dim t As String = tb.SelectedText    ' Copy the entire Text, if no text is selected.    If t.Length = 0 Then t = tb.Text    ' Proceed only if there is something to be copied.    If t.Length > 0 Then        Clipboard.SetDataObject(t)    End IfEnd Sub

The SetDataObject can take a second argument, which you should set to True if you want to make the copied object available after the current program terminates:

        ' Make the copied text available after the application ends.        Clipboard.SetDataObject(t, True)

Remember that you can put any object in the clipboard, including objects that are private to your application and that shouldn’t be accessed by other programs.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: