Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timer Inside Label

Tags:

vb.net

I am trying to put a countdown timer insde my label in my program, but when I run the program it doesn't countdown. It skips right to one, and that's it.

Private Sub CompactTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CompactTimer.Tick
    Dim Time As Integer = 11
    Do Until Time = 0
        ClockLabel.Text = "Compacting database in: " & Time
        Time -= 1
    Loop
End Sub

I also have started the timer and declared the interval to 500 in the Form_Load routuine.

like image 861
Chase Ernst Avatar asked Jun 15 '26 09:06

Chase Ernst


2 Answers

Get rid of the loop and declare the Time variable outside the scope.

Dim Time As Integer = 11

Private Sub CompactTimer_Tick(ByVal sender As Object, ByVal e As EventArgs) _
                              Handles CompactTimer.Tick
  If Time >= 0 Then
    ClockLabel.Text = "Compacting database in: " & Time
    Time -= 1
  Else
    CompactTimer.Stop
  End If
End Sub
like image 61
LarsTech Avatar answered Jun 18 '26 00:06

LarsTech


make a static var ..

Private Sub CompactTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CompactTimer.Tick
    Static Time As Integer = 11

    ClockLabel.Text = "Compacting database in: " & Time
    Time -= 1
    If Time = 0 Then CompactTimer.Stop

End Sub
like image 32
matzone Avatar answered Jun 18 '26 00:06

matzone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!