简体   繁体   English

VB.NET从列表框中选择链接,然后在Webbrowser控件中浏览到该链接

[英]VB.NET select link from listbox and browse to it in webbrowser control

I have a list of links stored in a listbox. 我有一个存储在列表框中的链接列表。 I want a timer control to load one link after the other every 4 seconds, and once it's reached the end of the list, I want the timer to stop. 我希望计时器控件每4秒在另一个链接之后加载一个链接,一旦到达列表的末尾,我希望计时器停止。 I've tried to achieve this with the code below and it's only loading the first link. 我试图通过下面的代码实现这一点,并且只加载第一个链接。 Can someone point out my mistake(s)? 有人可以指出我的错误吗? Thanks in advance! 提前致谢!

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

    Dim counter As Integer = counter + 1
    If counter > ListBox2.Items.Count Then
        counter = 0
        Timer1.Enabled = False
    Else
        Dim i As Integer
        For i = 0 To ListBox2.Items.Count - 1
            WebBrowser1.Navigate(ListBox2.Items(i))
        Next
    End If

End Sub

You need to declare your counter variable at the class level instead of within your Tick handler, or else its value is always reinitialized to 1. 您需要在类级别而不是在Tick处理程序中声明counter变量,否则其值始终会重新初始化为1。

So in other word, defined it oustide the handler. 因此,换句话说,定义它取代处理程序。

Also, you must not loop the entire content of the ListBox within your tick handler. 同样,您不得在刻度处理程序中循环ListBox的全部内容。

Here's a better version: 这是一个更好的版本:

Public Class Form1

    Private counter As Integer = 0

    ...

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        If counter > ListBox2.Items.Count - 1  Then
            counter = 0
            Timer1.Enabled = False
        Else
             WebBrowser1.Navigate(ListBox2.Items(counter))
        End If
        'Increment at the end to get element(0)
        counter = counter + 1

    End Sub
End Class

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM