Link to home
Start Free TrialLog in
Avatar of rowternet
rowternet

asked on

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.

Avatar of PaulHews
PaulHews
Flag of Canada image

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

Avatar of Nasir Razzaq
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
Avatar of rowternet
rowternet

ASKER

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
SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

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.
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"})
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can click "Accept and Award Points" on your own comment and award points to the experts as desired.