Ehi, sono un po 'nuovo su VB e sto cercando di ottenere unrichiesta web di aggiornare ogni 30 secondi e sono rimasto bloccato su come implementare un timer. Ecco il codice che ho prodotto finora, sarei grato per la soluzione corretta:
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
risposte:
1 per risposta № 1Il metodo che deve essere eseguito ogni certo periodo di tempo dovrebbe essere messo in
Timer1_Tick
evento pure. Questa è una piccola implementazione provala e vedi se funziona adattando il tuo codice di conseguenza:
Imposta le proprietà del timer desiderate:
Fare clic sul timer nella vista struttura e sul set di elenchi della casella delle proprietà:
- Proprietà intervallo su 30000 (l'evento si attiverà ogni 30 secondi)
- Abilitato su True (il timer inizierà a funzionare dopo il caricamento del modulo)
Quindi sul tuo Codebehind
private void ShowMessage()
{
MessageBox.Show("Hello Timer");
}
private void timer1_Tick(object sender, EventArgs e)
{
ShowMessage();
}
Anche qui c'è un'implementazione funzionante secondoal codice che hai pubblicato, per quanto ho capito dal tuo codice vuoi che il browser si aggiorni ogni determinati secondi e il controllo numerico su e giù mostra il valore della variabile impostata su boatId, questo codice fa questo:
Impostare le proprietà minime e massime del controllo numerico su giù nella casella Proprietà di esso (fare clic con il pulsante destro del mouse sul controllo in visualizzazione struttura e cercare queste due proprietà)
Quindi prova quanto segue;
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
Spero possa essere d'aiuto. Fammi sapere come va.