Clipboard Problems in VB.Net

skwerm
skwerm used Ask the Experts™
on
Greetings!

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
            Clipboard.SetDataObject(strX)
        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?

Thanks!

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer
Commented:
try using the full namespace to the Clipboard class:

System.Windows.Forms.Clipboard.SetDataObject(strX)
Ravi SinghSenior Software Engineer

Commented:
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
            txtText.SelectAll()
            txtText.Copy()
        Else
            txtText.Copy()
        End If

Author

Commented:
Zephyr:

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 Engineer

Commented:
Is there another class in your project named Clipboard? maybe theres a naming conflict?

Author

Commented:
That was it!

Many, many thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial