1

我需要有关 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
4

1 回答 1

0

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

当第一个完成时(documentcompleted 事件触发)然后加载下一个:

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
于 2013-07-09T10:29:25.707 回答