Home > VB.NET > How to Cut/Copy/Paste into the Clipboard using VB.NET

How to Cut/Copy/Paste into the Clipboard using VB.NET

In this example, I’ll show you how to cut, copy, paste and clear text in the clipboard. Cutting and copying text is really straight forward, pasting text requires just a little bit of checking. For example, we want to check to see what the clipboard contains before we try and do something like pasting an image into a text box.

To copy text, its just a simple one-liner:

Clipboard.SetText(txtTextBox.SelectedText)

To cut text, it’s almost as simple as copying it, but we need to remember to delete the data from the text box once it’s been copied:

Clipboard.SetText(txtTextBox.SelectedText)
txtTextBox.SelectedText = ""

To paste text there’s several things we have to worry about like I mentioned above. We first want to check what’s in the clipboard and verify that the contents of the clipboard can be pasted into whatever field/object we are trying to paste them into. In this example, I’m going to use the GetText method to make sure I only get the text from the clipboard and paste it into the text field. In the example below, I’ll get the data from the clipboard, then check to see if it is text before pasting it.

txtTextBox.SelectedText = Clipboard.GetText

Cut Copy Paste for .NET Applications

Cut Copy Paste for .NET Applications

Here are the names for each of the objects in the above photo, txtTextBox, btnCut, btnCopy, btnPaste each represent the text box, cut button, copy button, and paste button respectively. I’ll use the same variable names in each programming language below.

 

This is the code for the above program written in VB.NET

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'This code was developed by Gerardo Lopez and was downloaded from www.brangle.com
'Complete source code is available at:
'http://www.brangle.com/wordpress/2009/08/how-to-cutcopypaste-text-into-clipboard-using-vb-net/
 
Public Class Form1
    Private Sub btnCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCut.Click
        'Checks to see if the user selected anything
        If txtTextBox.SelectedText <> "" Then
            'Good, the user selected something
            'Copy the information to the clipbaord
            Clipboard.SetText(txtTextBox.SelectedText)
            'Since this is a cut command, we want to clear whatever 
            'text they had selected when they clicked cut
            txtTextBox.SelectedText = ""
        Else
            'If there was no text selected, print out an error message box
            MsgBox("No text is selected to cut")
        End If
    End Sub
 
    Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
        'Checks to see if the user selected anything
        If txtTextBox.SelectedText <> "" Then
            'Copy the information to the clipboard
            Clipboard.SetText(txtTextBox.SelectedText)
        Else
            'If no text was selected, print out an error message box
            MsgBox("No text is selected to copy")
        End If
    End Sub
 
    Private Sub btnPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPaste.Click
        'Get the data stored in the clipboard
        Dim iData As IDataObject = Clipboard.GetDataObject()
        'Check to see if the data is in a text format
        If iData.GetDataPresent(DataFormats.Text) Then
            'If it's text, then paste it into the textbox
            txtTextBox.SelectedText = CType(iData.GetData(DataFormats.Text), String)
        Else
            'If it's not text, print a warning message
            MsgBox("Data in the clipboard is not availble for entry into a textbox")
        End If
    End Sub
End Class

Now in general, I don’t think that the clipboard should be cleared by the programmer, it should be cleared by the user. But if you have a legitimate reason to clear the clipboard, here’s the code to do just that:

Clipboard.Clear()
Categories: VB.NET Tags: , , ,
  1. ogbonna
    March 12th, 2010 at 21:23 | #1

    Your note is quite explanatory and it’s very good.

  2. April 28th, 2010 at 22:02 | #2

    nice!

    Was wondering how you would copy a file to the clipboard?
    An example would be a text file?

    The next question would be how to check and past a file from the clipboard?

    I know how to copy/past/move/delete files but in my app I would like this to be done via the clipboard as well

    Cheers,
    Mark

  3. Manu
    June 13th, 2010 at 07:32 | #3

    Good Explanation.
    but can you elaborate on “How to paste string on Active window” that can be some other .exe running…

    Regards

  4. September 2nd, 2020 at 10:08 | #4

    I used to be able to find good info from your blog posts.

  5. September 2nd, 2020 at 10:20 | #5

    I go to see each day a few web sites and sites to read content, except this weblog
    presents quality based writing.

  1. November 8th, 2019 at 10:32 | #1
  2. November 16th, 2019 at 22:37 | #2
  3. November 16th, 2019 at 22:39 | #3
  4. December 7th, 2019 at 20:39 | #4
  5. December 13th, 2019 at 02:16 | #5
  6. December 23rd, 2019 at 03:46 | #6
  7. December 27th, 2019 at 12:52 | #7
  8. January 3rd, 2020 at 00:29 | #8
  9. January 8th, 2020 at 18:29 | #9
  10. January 8th, 2020 at 22:26 | #10
  11. January 8th, 2020 at 23:01 | #11
  12. January 9th, 2020 at 01:40 | #12
  13. January 9th, 2020 at 01:41 | #13
  14. January 9th, 2020 at 02:28 | #14
  15. January 9th, 2020 at 04:09 | #15
  16. January 9th, 2020 at 04:12 | #16
  17. January 9th, 2020 at 04:52 | #17
  18. January 9th, 2020 at 04:52 | #18
  19. January 9th, 2020 at 05:48 | #19
  20. January 9th, 2020 at 06:56 | #20
  21. January 9th, 2020 at 09:32 | #21
  22. January 9th, 2020 at 11:06 | #22
  23. January 9th, 2020 at 12:54 | #23
  24. January 9th, 2020 at 12:55 | #24
  25. January 9th, 2020 at 22:35 | #25
  26. January 12th, 2020 at 21:12 | #26
  27. January 16th, 2020 at 01:01 | #27
  28. January 19th, 2020 at 03:12 | #28
  29. January 21st, 2020 at 02:56 | #29
  30. February 16th, 2020 at 14:29 | #30
  31. February 16th, 2020 at 16:08 | #31
  32. February 16th, 2020 at 22:05 | #32
  33. March 7th, 2020 at 09:49 | #33
  34. March 8th, 2020 at 09:03 | #34
  35. March 8th, 2020 at 09:18 | #35
  36. March 10th, 2020 at 01:28 | #36
  37. March 10th, 2020 at 04:03 | #37
  38. March 10th, 2020 at 06:05 | #38
  39. March 12th, 2020 at 16:12 | #39
  40. March 14th, 2020 at 02:05 | #40
  41. March 17th, 2020 at 14:26 | #41
  42. April 2nd, 2020 at 04:38 | #42
  43. May 21st, 2020 at 12:25 | #43
  44. May 21st, 2020 at 17:52 | #44
  45. August 10th, 2020 at 06:01 | #45
  46. August 26th, 2020 at 21:47 | #46