• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 518
  • Last Modified:

How to tell when AxWebBrowser1 is finished loading?

I'm using VB.NET and I've got a button on a form with a webbrowser control on it.
On Form load it navigates to a site and logs in and goes to a specific page after login.
My goal is to click the button and then when the page finishes loading run some code to fill in the form.
I've already used "Private Sub AxWebBrowser1_DocumentComplete()" to auto negotiate past some other url's so the way I have it set up I cant really use it to tell when this page is done loading, so I have to find another way.

I've tried this but my "While" readystate part doesnt seem to work like it should.

How could I accomplish this?

Code:Private Sub btnFillCreditInquiry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFillCreditInquiry.Click

        While AxWebBrowser1.ReadyState = SHDocVw.tagREADYSTATE.READYSTATE_LOADING
        End While

        If AxWebBrowser1.ReadyState = SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE Then
            AxWebBrowser1.Document.Frames("Main").document.Forms(0)("app_first_name").Value = "FirstName"
        End If

    End Sub

The following is my DOCUMENTCOMPLETE code that makes it hard to use for the previous button click code.
--------------------------CODE -----------------------------------
   Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
        Static page As Integer = 1
        Select Case page
            Case 1
                AxWebBrowser1.Document.Forms("login").username.value = myLogin
                AxWebBrowser1.Document.Forms("login").password.value = myPassword

                page = page + 1     ' add one to the counter so it = 2

            Case 2

                page = page + 1     ' add one to the counter so it = 3

            Case 3
                Exit Sub
        End Select
    End Sub
1 Solution
Easy, create a public Property or member vairable in the AxWebBrowser class that tells you when the application is ready.

IE, add :
Public blnIsReady as Boolean = False
in the class definition, then in the AxWebBrowser1_DocumentComplete once all of your pages are loaded (case 3 at the moment) add:
Me.blnIsReady = True

Then in the containing class your loop would be:
        While AxWebBrowser1.blnIsReady = False
        End While
I would also suggest a call to something similar to the Sleep API call, as the tight loop with just DoEvents in it is a waste of the CPU.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now