0

嘿,我是 VB 的新手,我正在尝试每 30 秒刷新一次 Web 请求,并且一直坚持如何在其中实现计时器。这是我到目前为止生成的代码,我将不胜感激正确的解决方案:

Public Class main

Dim boatid As Integer



Sub googlemaps()
    Dim url As String = "http://www.google.com"
    Me.WebRequest.Navigate(New Uri(url))
    'Implement timer here? (me.refresh)?

End Sub

Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectBoat.ValueChanged
    boatid = SelectBoat.Value

    SelectBoat.Maximum = 10
    SelectBoat.Minimum = 1

    Lbboatid.Text = boatid
End Sub

Private Sub btnsequence_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsequence.Click
    Dim i As Integer
    boatid = i

End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshTimer.Tick
    RefreshTimer.Enabled = True
    RefreshTimer.Interval = 30000
End Sub

Private Sub main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Datetime.Text = Now.ToString

    googlemaps()
End Sub

结束类

4

1 回答 1

1

每隔一定时间必须执行的方法也应该放在 Timer1_Tick事件上。这是一个小的实现尝试一下,看看它是否可以相应地调整您的代码:

设置所需的 Timer 属性:

单击设计视图上的计时器及其属性框列表集:

  • 30000 上的间隔属性(事件将每 30 秒触发一次)
  • 启用 True(加载表单后计时器将开始工作)

然后在你的代码隐藏

private void ShowMessage() 
{           
  MessageBox.Show("Hello Timer");
}

private void timer1_Tick(object sender, EventArgs e)
{
  ShowMessage();
}

根据您发布的代码,这里还有一个工作实现,至于我从您的代码中理解的内容,您希望浏览器每隔特定秒刷新一次,并且数字上下控件显示在boatId 上设置的变量的值,这段代码是这样做的:

在它的“属性”框中设置数字上下控件的最小和最大属性(右键单击设计视图中的控件并搜索这两个属性)

然后尝试以下操作;

Public boatid As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.lblDate.Text = Now.ToString()
    ''Set a bigger winform to show the browser page better
    Me.Width = 800
    Me.Height = 600
    googlemaps()
End Sub

''The browser will be refreshed every n seconds) according to what you have set on interval property of the timer control
Sub googlemaps()
    Dim url As String = "http://www.google.com"
    Me.WebBrowser1.Navigate(New Uri(url))
End Sub

''Loop over this method using the Tick event, while doing that verify the value 
''of the boatid variable and if its less than the maximun value allowed by the numeric
''updown control increment it in one unit, then show it on the numeric selected value
''as well on the label control
Private Sub ChangeBoatIdValueCycling()

    If boatid < 10 Then
        boatid += 1
    Else
        boatid = 1
    End If

    Me.NumericUpDown1.Value = boatid
    Me.lblBoatId.Text = boatid.ToString()

End Sub

''This wil show the id on the label text when you click up and down the numeric control
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Me.lblBoatId.Text = Me.NumericUpDown1.Value.ToString()
End Sub

''This will set a variable with value 5 that will get shown selected on the numeric
''control as well as visible on the label text, the value will be shown because 
''it exists in the range of 1 to 10 that is your requeriment.
Private Sub btnsequence_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsequence.Click
    Dim i As Integer = 5
    boatid = i
    Me.NumericUpDown1.Value = boatid
    Me.lblBoatId.Text = boatid.ToString()
End Sub

''Needed to repeat the form refresh and boatid cycling every n seconds according to  your Interval property
''value
Private Sub RefreshTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshTimer.Tick
    googlemaps()
    ChangeBoatIdValueCycling()
End Sub

我希望它有所帮助。让我知道事情的后续。

于 2013-02-28T17:42:45.600 回答