Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings guys.. i am making a timer for my game using value from the database.
First, I converted the value from database assuming the value is 300. here is my code

VB
Public Class frmGame
   Public time As Integer
   Private Sub frmGame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dbConnect()
        rsGame = New ADODB.Recordset
        rsGame.Open("select * from Game", dbConnection, 1, 2)
        rsGame.MoveFirst()
        time = rsGame.Fields("TimePerLevel").Value
   End Sub

    Private Sub frmGame_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        lblMin.Text = time / 60
        lblSec.Text = "00"
        If MsgBox("Reach the Target Points to proceed to next level. ", MsgBoxStyle.OkOnly, "Notice") = MsgBoxResult.Ok Then
            Timer1.Enabled = True
        End If
    End Sub
    Sub cdtimer()
        Dim sec As Integer = 0
        Dim min As Integer = time / 60
        Dim less As Integer = 1
        lblMin.Text = min
        lblSec.Text = sec - less
        If lblSec.Text < 0 Then
            lblMin.Text = min - less
            lblSec.Text = 59
        End If
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        cdtimer()
    End Sub
End Class


i check the tutorials for timer on youtube and everything i did seems perfect.. but when i run the code. the value of my labels lblMin and lblSec is stuck at 4 and 59 respectively. Please help..
Posted

1 solution

Start with the debugger: put a breakpoint on the End Sub line of your frmGame_Load method, and look at exactly what is in time - I'm guessing it is 300.

But...after that you don't change the value! So the numbers you show never change.

You can just use
VB
time = time - 1
as the first line of your Timer1_Tick method, and that will work - but I'd do it differently.
Create a class level DateTime variable, and call it "endTime"
When you read the time from the DB, set endTime to the current time, plus the timer value:
VB
endTime = DateTime.Now.AddSeconds(time)
Then in your cdtimer method, compare the endtime and current time:
VB
Dim diff as TimeSpan = endTime - DateTime.Now
If diff.totalSeconds > 0 Then
   ' all ok
   lblMin.Text = diff.ToString("mm")
   lblSec.Text = diff.ToString("ss")
 
Share this answer
 
Comments
Daniel Breboneria Figueroa 26-Jul-15 3:51am    
i understand your logic sir.. it's like algebra
i just don't understand this line

lblMin.Text = diff.ToString("mm")
lblSec.Text = diff.ToString("ss")


where did you get the "mm" and "ss". sir?
OriginalGriff 26-Jul-15 4:08am    
If you don't understand something like that, then start with Google and MSDN.
Google for "TimeSpan.ToString" - because the object diff is a TimeSpan and the line is calling it's ToString method:
https://msdn.microsoft.com/en-us/library/dd992632(v=vs.110).aspx
That explains the overall, and links to here:
https://msdn.microsoft.com/en-us/library/ee372287(v=vs.110).aspx
Which explains what the string parameter means, and what you can do with it.
Read them, and you'll see that "mm" is "minutes, please" and "ss" is "seconds, if you'd be so kind".
Daniel Breboneria Figueroa 26-Jul-15 8:04am    
ok sir.. thank u very much.. ^_^
Daniel Breboneria Figueroa 27-Jul-15 2:51am    
sir.. im sorry to bother you again but.. i'm very sorry because i didn't tell you that it is not running that it causing me error from that line

lblMin.Text = diff.ToString("mm")
lblSec.Text = diff.ToString("ss")

Overload resolution failed because no accessible 'ToString' accepts this number of arguments.

i don't know if it is the framework version because i think this code is applicable to framework version 4.5 - 4.6..
I'm just using Visual Basic 2008 and i check the framework version that it is 3.5.
do you have an alternative solution to this?
I'm sorry sir if this cost you some time.. :(
OriginalGriff 27-Jul-15 5:50am    
Did you declare diff as a Timespan?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900