How to tell when AxWebBrowser1 is finished loading?

Posted on 2004-11-23
Last Modified: 2011-04-14
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
Question by:Matrix1000
    1 Comment
    LVL 7

    Accepted 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

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
    This is an explanation of a simple data model to help parse a JSON feed
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now