vb 2008 code problem

ringtemek

Daemon Poster
Messages
684
I'm making a countdown timer but after a number reaches zero it goes to negative numbers and i cant find the problem in my code

Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Sec.Text = Val(Sec.Text) - 1
        If Sec.Text = 0 Then
            Min.Text = Val(Min.Text) - 1
            Sec.Text = 59
            If Min.Text = 0 Then
                Hour.Text = Val(Hour.Text) - 1
                Min.Text = 59
            End If
        End If
    End Sub
 
Try assigning the values to a variable. Also make sure hour.text is not a zero.

Create a variable for sec.text, min.text, and hour.text. Then at the bottom, before you end sub, do this

Code:
Sec.text = intsec
Min.text = intmin
Hour.text = inthour

Those are just sample variable names.
 
what do you mean by "Create a variable for sec.text, min.text, and hour.text" i'm a beginner with programming
 
Oh I'm sorry. Ok. Here's what you have to do.

Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim intsec as integer
        Dim intmin as integer
        Dim inthour as integer
        
        intsec = val(sec.text)
        intmin = val(min.text)
        inthour = val(hour.text)
        intsec = intsec - 1
        If intsec = 0 Then
            intmin = intmin - 1
            intsec = 59
            If intmin = 0 Then
                inthour = inthour - 1
                intmin = 59
            End If
        End If

        sec.text = intsec
        min.text = intmin
        hour.text = inthour
    End Sub
[/quote]
 
it still does set it for 1 min 4 sec and after the second label goes to 59 the min label goes to 59 and the hour label goes to -1
 
let me review that code. All I did was do replacements.

Oh duh! Here I'll fix it for you.

Code:
    Option Explicit
        Dim timer as boolean (This will be a global variable, accessed from everywhere)
    
    Form_load() (there should be something like this when you double click the form)
        timer = true

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim intsec as integer
        Dim intmin as integer
        Dim inthour as integer

        
        intsec = val(sec.text)
        intmin = val(min.text)
        inthour = val(hour.text)

        If timer = true then
            intsec = intsec - 1
            If intsec = 0 and intmin = 0 and inthour = 0 Then
                timer = false
            else if intsec = 0 then
                if intmin > 0 then
                    intmin = intmin - 1
                    intsec = 59
                Else if intmin = 0 and inthour > 0 Then
                    inthour = inthour - 1
                    intmin = 59
                    intsec = 59
                End If
            End If
        end if

        sec.text = intsec
        min.text = intmin
        hour.text = inthour

(Else if might be elseif)
    End Sub
Ok, it's ready. I did some editing so if you saw this before this message update it.
 
Oo. Got me there. I haven't used VB in a while. Never used 08 either. All that I can tell you is to google it. Sorry man. I've been into C++ lately.
 
i found an error when i set the timer to 1 it goes to negative numbers and it skips all the minutes like if i set it to 1 min 1 sec it will go to 1 min 59 sec
 
Back
Top Bottom