Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 07-13-2008, 11:48 PM   #1
Daemon Poster
 
Join Date: Oct 2006
Posts: 684
Default vb 2008 code problem

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
__________________

ringtemek is offline   Reply With Quote
Old 07-13-2008, 11:53 PM   #2
Guru
 
JogaBonito1502's Avatar
 
Join Date: Jan 2008
Location: U.S.
Posts: 7,841
Default Re: vb 2008 code problem

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.
__________________

JogaBonito1502 is offline   Reply With Quote
Old 07-14-2008, 12:02 AM   #3
Daemon Poster
 
Join Date: Oct 2006
Posts: 684
Default Re: vb 2008 code problem

what do you mean by "Create a variable for sec.text, min.text, and hour.text" i'm a beginner with programming
ringtemek is offline   Reply With Quote
Old 07-14-2008, 12:10 AM   #4
Guru
 
JogaBonito1502's Avatar
 
Join Date: Jan 2008
Location: U.S.
Posts: 7,841
Default Re: vb 2008 code problem

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]
JogaBonito1502 is offline   Reply With Quote
Old 07-14-2008, 12:26 AM   #5
Daemon Poster
 
Join Date: Oct 2006
Posts: 684
Default Re: vb 2008 code problem

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
ringtemek is offline   Reply With Quote
Old 07-14-2008, 12:28 AM   #6
Guru
 
JogaBonito1502's Avatar
 
Join Date: Jan 2008
Location: U.S.
Posts: 7,841
Default Re: vb 2008 code problem

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.
JogaBonito1502 is offline   Reply With Quote
Old 07-14-2008, 01:04 AM   #7
Daemon Poster
 
Join Date: Oct 2006
Posts: 684
Default Re: vb 2008 code problem

thanks worked great how do I play a sound when the timer reaches 0
ringtemek is offline   Reply With Quote
Old 07-14-2008, 01:05 AM   #8
Guru
 
JogaBonito1502's Avatar
 
Join Date: Jan 2008
Location: U.S.
Posts: 7,841
Default Re: vb 2008 code problem

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.
JogaBonito1502 is offline   Reply With Quote
Old 07-14-2008, 10:59 AM   #9
Daemon Poster
 
Join Date: Oct 2006
Posts: 684
Default Re: vb 2008 code problem

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
ringtemek is offline   Reply With Quote
Old 07-14-2008, 02:05 PM   #10
Guru
 
JogaBonito1502's Avatar
 
Join Date: Jan 2008
Location: U.S.
Posts: 7,841
Default Re: vb 2008 code problem

Copy my code again. I'm not sure if you got the updated version. Tell me if that's sill doing it. It shouldn't.
__________________

JogaBonito1502 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 09:57 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0