|
-
Mar 4th, 2010, 07:57 AM
#1
Thread Starter
Hyperactive Member
DocumentCompleted fires twice?
Hi All,
I am trying to wait for the webbrowsers page to have finished loading to then carry out the next part of my code.
However i am having problems because the DocumentCompleted event fires twice?
I was just wondering why this is happening and if there's anyway to ignore the first event?
Many thanks,
Chloe ~X~
-
Mar 4th, 2010, 08:05 AM
#2
Re: DocumentCompleted fires twice?
vb Code:
WebBrowser1.Navigate("http://www.vbforums.com/showthread.php?t=605906")
While Not WebBrowser1.ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
End While
MsgBox("loaded")
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 4th, 2010, 09:01 AM
#3
Re: DocumentCompleted fires twice?
I have to disagree with Paul's suggestion. "Busy waiting" loops like that are never a good idea. If the DocumentCompleted event is raised twice it's because two documents are loaded. Most likely there's a frame in the main page. You can still test the ReadyState property but you do it in the DocumentCompleted event handler. It will be Complete the second time it's raised.
-
Mar 4th, 2010, 09:24 AM
#4
Thread Starter
Hyperactive Member
Re: DocumentCompleted fires twice?
Hi guys,
I tried Paul's method and it didn't resolve the issue i'm having, it made the program freeze for a long time.
This is my code, can you see any problems with it?:
vb Code:
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
If browserPage.Contains("Recruitment/AdManager/JobList.aspx") Then
For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input")
If input.GetAttribute("id") = "rptResults_ctl01_chkAction" Then
input.SetAttribute("checked", "true")
End If
If input.GetAttribute("name") = "btnCopyJob" Then
input.InvokeMember("click")
End If
Next
End If
End If
End Sub
I want to be able to wait till the second event to then run my code, at the moment it is trying to run my code on the first and second dcoumentCompleted events?
Many thanks,
Chloe ~X~
-
Mar 4th, 2010, 09:27 AM
#5
Re: DocumentCompleted fires twice?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 4th, 2010, 10:28 AM
#6
Thread Starter
Hyperactive Member
Re: DocumentCompleted fires twice?
Hi Paul,
What i want to happen is that when that page loads, the checkbox is checked and then a button is clicked.
At the moment my code is trying to tick the checkbox before the page is loaded and so i get an error from the page in a pop up saying i ahve not ticked the checkbox. If i then click ok on the msgbox the code then ticks the check box correctly and then clicks my button as it should? I just need it to wait a little longer before executing?
Thanks,
Chloe ~X~
-
Mar 4th, 2010, 10:33 AM
#7
Re: DocumentCompleted fires twice?
Is the WebBrowser control ever used by the User? or are you just using its methods to login in a site?
If the latter is true, then I would suggest ditching the WebBrowser control altogether and just use HTTPWebRequest.
You can accomplish the same things without the overhead of the WebBrowser control.
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Mar 4th, 2010, 11:15 AM
#8
Thread Starter
Hyperactive Member
Re: DocumentCompleted fires twice?
Hi,
Yes the webbrowser is being used by the user, the idea is i'm automating part of the task the user has to do manually?
Thanks,
Chloe ~X~
-
Mar 5th, 2010, 06:18 AM
#9
Thread Starter
Hyperactive Member
Re: DocumentCompleted fires twice?
Turns out it was my code :S!
When i seperated the action like below it worked fine:
vb Code:
For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input")
If input.GetAttribute("id") = "rptResults_ctl01_chkAction" Then
input.SetAttribute("checked", "true")
End If
Next
For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input")
If input.GetAttribute("name") = "btnCopyJob" Then
input.InvokeMember("click")
End If
Next
Thanks as always for your responses.
Chloe ~X~
-
Jun 18th, 2011, 06:26 PM
#10
New Member
Re: DocumentCompleted fires twice?
 Originally Posted by jmcilhinney
I have to disagree with Paul's suggestion. "Busy waiting" loops like that are never a good idea. If the DocumentCompleted event is raised twice it's because two documents are loaded. Most likely there's a frame in the main page. You can still test the ReadyState property but you do it in the DocumentCompleted event handler. It will be Complete the second time it's raised.
Just wanted to comment -- I found this solution via Google and it fixed a problem I've been having wit VB for ages. Thanks, jmcilhinney!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|