0

Hey im kind of new to VB and am trying to get a web request to refresh every 30 seconds, and have got stuck on how to implement a timer in to it. Here is the code I have produced so far, i would be grateful for the correct solution:

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

End Class

1 Answers1

1

The method that has to be executed every certain period of time should be put on the Timer1_Tick event as well. This is a small implementation try it and see if it works adapting your code accordingly:

Set up your desired Timer properties:

Click on the Timer on the Design View and on its Properties Box List set:

  • Interval property on 30000 (The Event will fire every 30 seconds)
  • Enabled on True (The Timer will start working after your form is loaded)

Then on your Codebehind

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

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

Also here is a working implementation according to the code you posted, as for what i understand out of your code you want the browser to refresh every certain seconds as well as the numeric up and down control show the value of the variable set on boatId, this code does that:

Set the minimum and maximum properties of your numeric up down control on the Properties box of it (right click on the control in design view and search for those two properties)

Then try the following;

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

I hope it helps. Let me know how it goes.

CoderRoller
  • 1,239
  • 3
  • 22
  • 39
  • Yeah that worked great thanks. The other issue i have is that i want the sequence button to adjust the boatid value from 1 to 9 every 30 seconds and cycle. As well as the user having the option to change the boat ID with the numericupdown. –  Mar 01 '13 at 12:16
  • Check the update @mdcuk, if you don't understand anything let me know. Keep on coding, reading and learning. Thanks. – CoderRoller Mar 01 '13 at 15:17