webbrowser1_documentcompleted is firing multiple times

Hi,

The webbrowser1_documentcompleted is firing more than once(3 times).
Eventhough i am checking for ready state to be completed, it is not working.
I am using  this code:

Try
            If (WebBrowser1.ReadyState <> WebBrowserReadyState.Complete) Then
                exit sub
            Else
                WebBrowser1.Document.InvokeScript("GetMap", New String() {"addr1", "addr2"})
            End If

Any suggestions on how to prevent it firing more than once?

Thanks.

rowternetAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
rowternetConnect With a Mentor Author Commented:
Hi Paul,

I did not get a a chance to look at them.
Previously all the Javascript functions werelinked. That is i call one function from the wbbrows and that function cals anther function and so on.
I split theJavascript into multiple functions and calling each function in a different button click.
Right now this is working. great We are goign ahead with what we havenow. But, i would revisit this some time later.


I would award the points to you.
How can i do that without accepting solution? Can this be done?

Thanks
0
 
PaulHewsCommented:
If there are frames, it will fire for each frame.  One standard trick to trap when it has loaded everything is to check the URL property:
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted 
    If e.Url = WebBrowser1.Url Then
        MsgBox("Document Completed")
    End If 
End Sub

Open in new window

0
 
CodeCruiserCommented:
Use the ReadyState

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
       'Completed
    End If
End Sub
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
rowternetAuthor Commented:
Hi,

Using e.url = webbrowser1.url  ----- It does not fire multiple times. But iit is blocking the parameters sent to my map. For some reason the method is not seeing the parameters if i include this line

Using webbrowser1.readystate = webbrowserreadystate.complete  ------ This is not helping. It is firing multiple times. But mymap and other methods are executing properly. This multiple execution is causing a problem as the map is displaying  the same location multiple times.

Any other way to do this?

Thanks
0
 
PaulHewsConnect With a Mentor Commented:
I am thinking the only other way to do this would be to use some flag to indicate that a location has been added and don't add it again.  It's a hack, but it sounds like there's some odd redirection issue or some other issue that's causing the behaviour, which is not normal.

If for example, the addr1 and addr2 together are supposed to be unique then see code below.

Even better would be if there was a way to check if specific locations had already been added, then don't add them.
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
	Static MyParms As New List(Of String)

	If (WebBrowser1.ReadyState <> WebBrowserReadyState.Complete) Then
		Exit Sub
	Else
		If Not MyParms.Contains("addr1" & "addr2") Then
			MyParms.Add("addr1" & "addr2")
			WebBrowser1.Document.InvokeScript("GetMap", New String() {"addr1", "addr2"})
		End If

	End If
End Sub

Open in new window

0
 
rowternetAuthor Commented:
Hi PaulHews,

I tried the above one. It did not work.
I have 2 questions:
1) How will i know how many frames are there on the document?
2) Can i call the browser.document.invokescript in the form_load event? Or should i call it only in browser_documentcompleted event?
I tried to call it i the form_load event but it is not working. I need a confimation on this.

Thanks.

0
 
PaulHewsCommented:
1.  Are you sure frames are being used?  Have you looked at the HTML source of the pages you are loading?

Try:
WebBrowser1.Document.Window.Frames.Count

2.  Form_Load is an event when your windows form loads.  Seems unlikely you could do anything useful here.

>I tried the above one. It did not work.

If something doesn't work, you have to give a better picture of exactly what was tried and how it doesn't work.
0
 
PaulHewsCommented:
Note that if your script is in a frame, you would need to invoke it from the right document... eg:

WebBrowser1.Document.Window.Frames("frame_with_script").Document.InvokeScript("GetMap", New String() {"addr1", "addr2"})
0
 
PaulHewsCommented:
You can click "Accept and Award Points" on your own comment and award points to the experts as desired.
0
All Courses

From novice to tech pro — start learning today.