Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to send Control commands to Webbrowser control 1

Status
Not open for further replies.

GPerk

Programmer
Jul 6, 2002
161
US
I can manually do a Control-A to select all the text on a WebBrowser control, and a Control-C to copy it to the clipboard.
But...
How can I get my VB.NET program to send AUTOMATICALLY a command (such as Control-A or Control-C) to the WebBrowser control?
 

The SendKeys function can be used to send keys to the current active control in your application. Do this:

WebBrowser1.Focus()

SendKeys.Send("^a") 'ctrl+a
SendKeys.Send("^c") 'ctrl+c

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Jebenso,
Thank you! That does the job!

Here is my sub:

Private Sub WB_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WB.DocumentCompleted
Clipboard.Clear()
WB.Focus()
SendKeys.SendWait("^a") ' select
SendKeys.SendWait("^c") ' copy

txtData.Focus()
SendKeys.Send("^v") ' paste
End Sub

However, the DocumentCompleted sub appears to be getting executed more than once - the data appears repeated in txtData 2 or 3 times.
Perhaps I need to set a switch to stop it after one time?
 

The DocumentComplete event fires for every document that gets loaded into the WebBrowser control. So if the URL you are navigating to has frames, etc., you will get a DocumentComplete event fired for each one. To determine when all documents have finished loading completely, use the ReadyState property of the WebBrowser control:

If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
Clipboard.Clear()
WB.Focus()
SendKeys.SendWait("^a") ' select
SendKeys.SendWait("^c") ' copy

txtData.Focus()
SendKeys.Send("^v") ' paste
End If

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Jebenson,
Thanks for the info.
Works perfectly!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top