Solved

WebBrowser doesnt load during loop

Posted on 2011-03-01
7
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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 …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

626 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