Results 1 to 24 of 24

Thread: Timer

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    216

    Timer

    I now this is simple but how do you make the timer countdown from... 5 minutes and show how much time is left in label1? Also, is there command that will exit the application (for ex.) when the count = 0?

    TIA

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    For exiting your application use this code .

    VB Code:
    1. Application.Exit()

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    216
    No, i mean when the clock is at 0

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by james14
    No, i mean when the clock is at 0
    Hmm , you've just said :
    Also, is there command that will exit the application (for ex.) when the count = 0?
    anyways , when the clock is at 0 what do you want to do ? exit the timer ? or the application ? or if you have loop and you want to terminate that loop ?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    216
    heh sorry, what i want it to do is when the clock hits 0 (time runs out) the application will exit itself because it ran out of time.

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Ok . This counts 10 sec before it exits itself .

    VB Code:
    1. Dim sec As Integer = 10
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Timer1.Enabled = True
    5.     End Sub
    6.  
    7.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    8.         sec -= 1
    9.         If sec > 0 Then
    10.             Label1.Text = sec
    11.         Else
    12.             Timer1.Enabled = False
    13.             Application.Exit()
    14.         End If
    15.     End Sub

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Note , Leave the timer set to 1000 for interval and enabled property to false .

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    How often do you want the timer to tick down? Every second? Every Minute?

    Nevermind Pirate's got your back.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    216
    Thanks guys!

  10. #10
    Lively Member
    Join Date
    Oct 2004
    Posts
    123
    Originally posted by Pirate
    Ok . This counts 10 sec before it exits itself .

    VB Code:
    1. Dim sec As Integer = 10
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Timer1.Enabled = True
    5.     End Sub
    6.  
    7.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    8.         sec -= 1
    9.         If sec > 0 Then
    10.             Label1.Text = sec
    11.         Else
    12.             Timer1.Enabled = False
    13.             Application.Exit()
    14.         End If
    15.     End Sub
    This is good. but how can i got problem using my method.
    I tried Dim sec As Integer = NumericUpDown1.Text but it seems to have problem and prompt me

    An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication2.exe

    Additional information: Object reference not set to an instance of an object.

    but all those are in the NumericUpDown box are numbers? Why?? Please help
    The Power Of Sharing Knowledge!

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This is old thread so I'm not gonna read it over , with Numeric controls use

    NumericUpDown1.Value (not text) .

    This would save you the hastle of casting . I don't see a problem here though .

  12. #12
    Lively Member
    Join Date
    Oct 2004
    Posts
    123
    Originally posted by Pirate
    This is old thread so I'm not gonna read it over , with Numeric controls use

    NumericUpDown1.Value (not text) .

    This would save you the hastle of casting . I don't see a problem here though .
    It give me the same error too. Why? Or should I use a differnent method but not numericupdown box?
    The Power Of Sharing Knowledge!

  13. #13
    Lively Member
    Join Date
    Oct 2004
    Posts
    123
    Please help me? This maybe simple but I just can't understand both method it doesn't work for me? Or no one understand my question?
    The Power Of Sharing Knowledge!

  14. #14
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by private1337
    This is good. but how can i got problem using my method.
    I tried Dim sec As Integer = NumericUpDown1.Text but it seems to have problem and prompt me

    An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication2.exe

    Additional information: Object reference not set to an instance of an object.

    but all those are in the NumericUpDown box are numbers? Why?? Please help
    Look carefully at where you are getting that error message. Have you declared Sec with too narrow a scope?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  15. #15
    Lively Member
    Join Date
    Oct 2004
    Posts
    123
    Originally posted by taxes
    Look carefully at where you are getting that error message. Have you declared Sec with too narrow a scope?
    What u mean? I tried put Dim sec As Integer = NumericUpDown1.Text or Dim sec As Integer = NumericUpDown1.Value on top of the form codings and

    Dim secs As Integer = NumericUpDown4.Value
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer10.Tick
    secs -= 1
    If secs > 0 Then
    lbl.Text = secs
    Else
    Timer1.Enabled = False
    End If
    End Sub

    I just don't understand why there is an error? Or I put at the wrong place?
    The Power Of Sharing Knowledge!

  16. #16
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Right. Your scope is OK but you have to assign the values after the NumericUpDown value has been selected. Presumably the NumericUpDown is contained in the same form so you need

    VB Code:
    1. Private Sub NumericUpDown4_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
    2.  
    3.    secs =CInt(NumericUpDown4.Value)    
    4. End Sub

    However, as you are using several NumericUpDown controls, you probably need to include that code in the click event of a Proceed button.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  17. #17
    Lively Member
    Join Date
    Oct 2004
    Posts
    123
    So sad to say it give me the same error too? Got other way?
    The Power Of Sharing Knowledge!

  18. #18
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Post a sample of your proj .

  19. #19
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    I hope you are not confusing "sec" with "secs". If you are using both make sure botah are correctly instanced. Exactly which line gives you the error?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  20. #20
    Lively Member
    Join Date
    Oct 2004
    Posts
    123
    Hi, I have not mistake the sec and secs. I tried try in new form but it also doesn't work.. My code is

    Dim sec As Integer = NumericUpDown1.Value
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Timer1.Enabled = True
    Timer1.Interval = 1000
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    sec -= 1
    If sec > 0 Then
    Label1.Text = sec
    Else
    Timer1.Enabled = False
    Application.Exit()
    End If
    End Sub

    Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
    sec = CInt(NumericUpDown1.Value)
    End Sub
    End Class

    The error is Dim sec As Integer = NumericUpDown1.Value
    The Power Of Sharing Knowledge!

  21. #21
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Please read the responses you get carefully.

    You have already been told that
    Dim sec As Integer =NumericUpDown1.Value

    is not acceptable for two reasons;

    1. It should be simply Dim Sec As Integer


    2. Even if you do try to set it a value at that time it should be

    Dim sec As Integer = CInt(NumericUpDown1.Value)

    The intellisense tries to tell you just that!
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  22. #22
    Lively Member
    Join Date
    Oct 2004
    Posts
    123

    Resolved [ResolveD]

    Originally posted by taxes
    Hi,

    Please read the responses you get carefully.

    You have already been told that
    Dim sec As Integer =NumericUpDown1.Value

    is not acceptable for two reasons;

    1. It should be simply Dim Sec As Integer


    2. Even if you do try to set it a value at that time it should be

    Dim sec As Integer = CInt(NumericUpDown1.Value)

    The intellisense tries to tell you just that!
    It solve the problem.. How stupid can I be Thanks anyway..
    The Power Of Sharing Knowledge!

  23. #23
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    We got there in the end. Best of luck.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  24. #24
    Lively Member
    Join Date
    Oct 2004
    Posts
    123
    Thanks taxes.. u r the Timer pro
    The Power Of Sharing Knowledge!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width