Question My.Computer.Clipboard

pisceswzh

Well-known member
Joined
Mar 19, 2007
Messages
96
Programming Experience
1-3
Hi,

I am working on a program of extracting data from the clipboard.

The data in the clipboard is actaully copied from the WebBrowser control. (User highlighs certain text part and press Ctrl+C)

However I cannot extract from the clipboard using My.Computer.Clipboard.GetText or My.Computer.Clipboard.GetData.

Anyone can help me?
 
You may want to be more specific, because that normally works as expected.
 
There is a WebBrowser control on the form and in that WebBrowser control displayed a web page, let's say it Yahoo and in that Yahoo page, user highlights some texts and press Ctrl+C on the keyboard. So the texts that is highlighted should be in the clipboard, right? Actually it is in the clipboard, since if at that moment, I can use Ctrl+V to extract the data. However even I know there is some texts in the clipboard, I cannot use My.Computer.Clipboard.GetText to extract the data in it. This might be because the data in the clipboard is in HTML format or something. So I tried to use My.Computer.Clipboard.GetData. However the GetData method need some format string and I don't know which string I should use. I tried several, but none of them works.

So the current problem might be how to extract HTML content from the clipboard? Please help. Thanks.
 
I went to Yahoo too, but I still see no problem with GetText. You can always GetDataObject and look into that by its GetFormats method.
 
Oh really? You can use GetText to extract the data from the clipboard? Can you copy and paste the code?

btw, instead of let the user actually press the Ctrl+C. I asked the user press F1 and then in the PreviewKeyDown event using SendKeys() to send a Ctrl+C and try to paste them in a textbox.

The SendKeys() part works fine, since I can use Ctrl+V to manually paste the content. However I cannot use GetText() to extract them from the clipboard and display them in a textbox.

So please kindly show me the code about the GetText() part, thanks.

Tom
 
ClipBoard.GetText. No really, I have no problem with it after ctrl-c from any webpage.
 
OK. Following is my code. Actually is very simple. I can't tell what's really the problem.

Private Sub webCV_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles webCV.PreviewKeyDown
  If e.KeyCode = Keys.F1 Then
    My.Computer.Clipboard.Clear()
    My.Computer.Keyboard.SendKeys("^c")

    MessageBox.Show(My.Computer.Clipboard.GetText())
  End If
End Sub

The Messagebox I get if I run this piece of code is BLANK.

Thanks

Tom
 
I think you're jumping the gun too fast there, Clipboard is a COM component and I've witnessed it being quite slow in other situations, so you'd be lucky if anything copied from your sendkeys call will be in clipboard by the time GetText is called here. The default SendKeys also just dispatches the message and continues without waiting for it to complete. So when you said user pressed ctrl-c, did that mean you were trying to catch those keypresses with PreviewKeyDown immediately too? If so, same problem, slow clipboard. I was pressing ctrl-c then I clicked a button that retrieved the clipboard. Anyhow, when you need to copy to clipboard selected text in WebBrowser and get it right away I suggest you use the ExecCommand with the "Copy" command like this:
VB.NET:
Private Sub WebBrowser1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles WebBrowser1.PreviewKeyDown
    If e.KeyCode = Keys.F1 Then
        Me.WebBrowser1.Document.ExecCommand("Copy", False, Nothing)
        MessageBox.Show(Clipboard.GetText)
        e.IsInputKey = True 'prevents event raised twice
    End If
End Sub
 
I tried, but no luck. I tried several times and there is only one time it really worked. Now I think it does relate to the delay of the clipboard.

On the other hand, your method cause another problem - "Requested Clipboard peration did not succeed." and I don't know why.
 
I am upload an image to show what I want to do and please help to see if there is any other solution?
 

Attachments

  • 1.PNG
    1.PNG
    37 KB · Views: 25
I tried, but no luck. I tried several times and there is only one time it really worked. Now I think it does relate to the delay of the clipboard.

On the other hand, your method cause another problem - "Requested Clipboard peration did not succeed." and I don't know why.
I tested the posted code many times without problem. The error message is not known to me, but I see it some hits about it when searching, you should look into those to check if there could be an underlying problem and resolution for your clipboard.
 
Back
Top