devxlogo

Copying data into the Clipboard

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.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist