Solved

WebBrowser doesnt load during loop

Posted on 2011-03-01
7
424 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

22 Experts available now in Live!

Get 1:1 Help Now