Solved

WebBrowser doesnt load during loop

Posted on 2011-03-01
7
435 Views
Last Modified: 2012-05-11
I am using the following code to  open a webpage

WebBrowser1.Navigate("https://exweb.exchange.uk.com/services/quote.aspx", "", bytePostData1, strHeaders)

because the page doesnt load in time to be used later on in the code i use the following code to wait until the page has finished loading

        Do Until WebBrowser1.DocumentText <> ""
        Loop

However for some reason this loop seems to go on forever (i.e. the page never finishes loading) unless i put a message box contatining the document text in the loop (i dont wnat to do this on the live program).

i ahve also tried putting application.doevents() in the lop but that doesnt work either.

Does anyone have any ideas?
0
Comment
Question by:ict-torquilclark
  • 4
  • 3
7 Comments
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 35006444
Hi,

Instead of looping you can use DocumentCompleted Event which Occurs when the WebBrowser control finishes loading a document.  For details:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx

You can further process once this event is fired - making sure that document is loaded.

Let me know if you need more help.
ASim
0
 
LVL 1

Author Comment

by:ict-torquilclark
ID: 35006617
i have decalred the webbrowser in my module liek this
Dim WebBrowser1 As New System.Windows.Forms.WebBrowser

dow do i create a document complete event for this control??
0
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 35006729
Here is the code:

 
Private Sub PrintHelpPage()

    ' Create a WebBrowser instance. 
    Dim webBrowserForPrinting As New WebBrowser()

    ' Add an event handler that prints the document after it loads.
    AddHandler webBrowserForPrinting.DocumentCompleted, New _
        WebBrowserDocumentCompletedEventHandler(AddressOf PrintDocument)

    ' Set the Url property to load the document.
    webBrowserForPrinting.Url = New Uri("\\myshare\help.html")

End Sub

Private Sub PrintDocument(ByVal sender As Object, _
    ByVal e As WebBrowserDocumentCompletedEventArgs)

    Dim webBrowserForPrinting As WebBrowser = CType(sender, WebBrowser)

    ' Print the document now that it is fully loaded.
    webBrowserForPrinting.Print()
    MessageBox.Show("print")

    ' Dispose the WebBrowser now that the task is complete. 
    webBrowserForPrinting.Dispose()

End Sub

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:ict-torquilclark
ID: 35007258
I now have the following code in my module

        Dim WebBrowser1 As New System.Windows.Forms.WebBrowser
        AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf test)

        If QuoteFromLiveExchange = True Then
            WebBrowser1.Navigate("https://exweb.exchange.uk.com/services/quote.aspx", "", bytePostData1, strHeaders)
        Else
            WebBrowser1.Navigate("https://ifapreview.exchange.uk.com/services/quote.aspx", "", bytePostData1, strHeaders)
        End If


i have created a function called test with just a message box in the code to test the concept. i run the code and nothing happens :(
0
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 35007578
First try to run the code I pasted i.e. by pasting it in a new form. Do this:

Add a new form
Place browser control on it
In the form designer, select this browser control and see properties.
Select Events tab in the property window and double click DocumentCompleted event.
This will add event handler for you.
Now run above code. Once this works for you then switch to your way of implementation.

This will helps you in understanding what you want todo.

Asim
0
 
LVL 1

Author Comment

by:ict-torquilclark
ID: 35007613
I understand how to add a document completed event handeler for a control that i have created automatically by dragging it onto a form

the web browser i am using thought has been created manually by using

dim webbrowser 1 as new webbrowser()

it is not actually a physical control in a form.
0
 
LVL 10

Accepted Solution

by:
Asim Nazir earned 500 total points
ID: 35007842
Ok. Here is working code:

 
Public Class Form1

    Dim WebBrowser1 As New System.Windows.Forms.WebBrowser


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf test)

        If True Then
            WebBrowser1.Navigate("https://exweb.exchange.uk.com/services/quote.aspx")
        Else
            WebBrowser1.Navigate("https://ifapreview.exchange.uk.com/services/quote.aspx")
        End If

    End Sub

    Private Sub test(ByVal sender As Object, _
        ByVal e As WebBrowserDocumentCompletedEventArgs)

        Dim webBrowserForPrinting As WebBrowser = CType(sender, WebBrowser)

        ' Print the document now that it is fully loaded.
        webBrowserForPrinting.Print()
        MessageBox.Show("print")

        ' Dispose the WebBrowser now that the task is complete. 
        webBrowserForPrinting.Dispose()

    End Sub

End Class

Open in new window


Add Form1 to your solution and replace form1.cs code with attached one and see how it goes.
Asim
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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