2013-02-28 72 views
0

嘿,這是一種新的VB,我試圖獲得一個Web請求每30秒刷新一次,並陷入瞭如何實現一個計時器。下面是到目前爲止我已經產生的代碼,我將是正確的解決方案感激:VB.NET 2010 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 

末級

回答

1

具有要被執行的每隔一定時間應在 放的方法Timer1_Tick事件也是如此。這是一個小的實現嘗試一下,看看它是否工作相應地調整你的代碼:

設置你想要的定時器特性:

點擊計時器上的設計視圖,並在其屬性框列表設置:

  • 上30000(本次活動將每隔30秒射擊)
  • 啓用的真Interval屬性(計數器將開始工作表單加載後)

然後在你的代碼隱藏

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

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

而且,這裏是按照您發佈,至於什麼我明白了你的代碼,你想刷新瀏覽器每隔一定的秒以及數字組成的代碼工作實現和down控制顯示在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 

我希望它有幫助。讓我知道事情的後續。

+0

是的,非常感謝。我遇到的另一個問題是,我希望序列按鈕每隔30秒調整一次boatid值從1到9並循環。以及用戶可以選擇使用數字下拉來更改船隻ID。 – mdcuk 2013-03-01 12:16:04

+0

檢查更新@mdcuk,如果你不明白什麼讓我知道。 繼續編碼,閱讀和學習。謝謝。 – CoderRoller 2013-03-01 15:17:14

+0

嗨@mdcuk,答案是怎麼回事。它沒有工作嗎? – CoderRoller 2013-03-04 14:06:00