VB等待网页加载

时间:2013-07-09 10:03:19

标签: .net vb.net webbrowser-control webpage wait

我需要有关VB等待网页加载的帮助。 点击一下按钮=>浏览一个页面并等待加载,然后更改一个标签文本,然后导航另一个页面并等待加载,然后更改另一个标签文本,然后导航并等待加载另一个页面,然后更改标签文本。请帮助我,我尝试使用此代码,但失败了......

Private Sub Button1_Click ...
WebBrowser1.Navigate("page1")
Label1.Text = "Loaded" 'but this show before the page loaded
WebBrowser1.Navigate("page2") 'this start loading before the page1 loaded
Label2.Text = "Loaded" 'but this show before the page loaded
WebBrowser1.Navigate("page3") 'this start loading before the page1 loaded
Label3.Text = "Loaded"
...
End Sub

1 个答案:

答案 0 :(得分:0)

您只需要跟踪正在加载的页面。您可以使用Static变量执行此操作。

当第一个完成时(文档已完成的事件触发)然后加载下一个:

Private Sub Button1_Click
    'Load the first page
    LoadNextPage
End Sub

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    'When the current page has finished loading - load the next page
    LoadNextPage
End Sub

Private Sub LoadNextPage
    Static page As Integer = 0 'set to page number we are loading
    'increment the page count
    page += 1
    'Load the appropriate page
    Select Case page
        Case 1
            WebBrowser1.Navigate("page1")
        Case 2
            Label1.Text = "Loaded 1 now loading 2" 
            WebBrowser1.Navigate("page2") 
        Case 3
            Label2.Text = "Loaded 2 now loading 3" 
            WebBrowser1.Navigate("page3") 
        Case Else
            Label3.Text = "Loaded All Documents"
    End Select
End Sub