Clipboard Problems in VB.Net


I'm simply trying to copy/paste plain text strings from/to the clipboard. I've found some sample code on the internet (and here at EE) and it seems like it should be straight-forward. Strangely enough, my Clipboard object doesn't seem to have the methods these examples use, in particular SetDataObject() and GetDataObject().

I've got this code:

Private Sub BtnCopyToClipboardClick(sender As System.Object, e As System.EventArgs)
           'Copy the selected text to the clipboard
        Dim strX As String = txtText.SelectedText
        'if no text is selected, copy the entire text
        If strX.Length = 0 Then strX = txtText.Text
       'Copy data if any to copy
        If strX.Length > 0 Then
        End If

And I get this error:
  error BC30456: 'SetDataObject' is not a member of 'Clipboard'.

The only methods that show up on my clipboard object are: Equals, Finalize, GetHashCode, GetType, and ToString.

Any idea what's going on?


Who is Participating?
Ravi SinghConnect With a Mentor Senior Software EngineerCommented:
try using the full namespace to the Clipboard class:

Ravi SinghSenior Software EngineerCommented:
You can also use the Copy method on the textbox to accomplish what your after, the following will copy all the text in the textbox if none is selected else it will copy the selected text:

        If txtText.SelectedText.Trim() = String.Empty Then
        End If
skwermAuthor Commented:

When I use the entire namespace I do get the missing methods on the Clipboard object. So the problem is fixed.

Any idea why I needed the full namespec? I have "Imports System.Windows.Forms" at the top of my form's source code.

Ravi SinghSenior Software EngineerCommented:
Is there another class in your project named Clipboard? maybe theres a naming conflict?
skwermAuthor Commented:
That was it!

Many, many thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.