循环使用Webbrowser VB.Net

时间:2016-08-11 17:10:20

标签: vb.net

dim web1 as string 
dim web2 as string
dim i as int32

web1 = "www.webaddress1.com"
web2 = "www.webaddress2.com"
web3 = "www.webaddress3.com"

i = 1

while i < 100

webbrowser1.navagate(web1)

i = i + 1

end while 

我有很多网站不确定如何在每个循环开始时更改网站,因此它将通过web1 web2 web3等。

2 个答案:

答案 0 :(得分:1)

现在,你所做的所有代码都是99次访问同一个网站。您没有通过任何新的网站变量。就像Plutonix所说,你需要遍历一个集合,看起来像这样:

    Dim lstWebsites As New List(Of String) From {"www.webaddress1.com", _
                                                 "www.webaddress2.com", _
                                                 "www.webaddress3.com"}
    For Each website As String In lstWebsites
        webbrowser1.navigate(New Uri(website))
    Next

但是要小心,这样的循环会很快执行,所以根据你要点击的链接,你可能希望用计时器将它们分开,或者在DocumentCompleted事件之后执行每一个

编辑 - 回应评论:如果我在自己的项目中这样做,我宁愿使用DocumentCompleted事件而不是计时器,这样一来,这个过程花费尽可能少的时间来完成运行,并且每个访问的地址都给出了它总是需要完成加载。那说,原则上,这看起来像这样:

Public Class Form1
    Private Property lstWebsites As List(Of String)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        lstWebsites = New List(Of String) From {"www.webaddress1.com", _
                                                "www.webaddress2.com", _
                                                "www.webaddress3.com"}
        NavigateNext()
    End Sub

    Private Sub NavigateNext()
        If lstWebsites IsNot Nothing AndAlso lstWebsites.Count > 0 Then
            WebBrowser1.Navigate(New Uri(Me.lstWebsites(0)))
            lstWebsites.RemoveAt(0)
        Else
            Me.Close()
        End If
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        NavigateNext()
    End Sub
End Class

答案 1 :(得分:-1)

如果你坚持不使用列表(如soohoonigan建议的那样),因为你还没有学过它们,你可以随时调用导航功能三次。请注意,列表解决方案更正确,我只是提供替代方案。

dim web1 as string 
dim web2 as string
dim i as int32

web1 = "www.webaddress1.com"
web2 = "www.webaddress2.com"
web3 = "www.webaddress3.com"

i = 1

while i < 100

    webbrowser1.navigate(web1)
    webbrowser1.navigate(web2)
    webbrowser1.navigate(web3)

    i = i + 3

end while 

免责声明:未经测试的代码