Solved

WebBrowser doesnt load during loop

Posted on 2011-03-01
7
421 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

20 Experts available now in Live!

Get 1:1 Help Now