Solved

WebBrowser doesnt load during loop

Posted on 2011-03-01
7
437 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…

739 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