Results 1 to 11 of 11

Thread: [RESOLVED] [02/03] Timer Counter

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    19

    Resolved [RESOLVED] [02/03] Timer Counter

    I am trying to code a counter in my game so the player has 15 seconds to answer the question. I have tried many different codes but all of them do things I don't want, like it counts down but only shows the 0 when finished counting down. It doesn't show the countdown from 15 to 1 on the form.

    Here is my code:

    Dim counter As Integer = 0
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Me.uiCounterLabel.Text = counter
    counter = counter - 1
    If counter <= 0 Then
    Timer1.Enabled = False
    counter = 15
    End If
    'clear out question and answer
    uiQuestTextBox.Text = ""
    uiAnsLabel2.Text = ""
    'message to player that time has elapsed
    MessageBox.Show("Your time is up!")
    'go back to main form
    Main1.Show()
    Me.Hide()

    End Sub

    I must be doing something wrong and hope someone can assist.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,383

    Re: [02/03] Timer Counter

    here is an example that should guide you in the right direction

    its commented to show you whats going on
    Attached Files Attached Files

  3. #3
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [02/03] Timer Counter

    VB Code:
    1. Private WithEvents tmr As New System.Timers.Timer(1000)
    2.     Private SecondRemain As Int32 = 15
    3.  
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         Me.label1.text = "Who is the coolest guy ever?"
    6.         Me.Label3.Text = "Seconds Remaining: " & SecondRemain
    7.         tmr.Start()
    8.     End Sub
    9.  
    10.     Private Sub tmr_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tmr.Elapsed
    11.         If Not SecondRemain = 0 Then
    12.             SecondRemain -= 1
    13.             Me.Label3.Text = "Seconds Remaining: " & SecondRemain
    14.         Else
    15.             MessageBox.Show("You're just jealous...")
    16.             tmr.Stop()
    17.         End If
    18.     End Sub
    19.  
    20.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    21.         If Not Me.TextBox1.Text.Trim.ToLower = "sevenhalo" Then
    22.             Me.Label2.Text = "WRONG"
    23.         Else
    24.             Me.Label2.Text = "Correct!"
    25.             tmr.Stop()
    26.         End If

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    19

    Re: [02/03] Timer Counter

    kleinma: It still does not work. It could be that I am putting the code in the wrong places.

    sevenhalo: There seems to be alot of extra code in your answer that is not helping me with the problem.

  5. #5
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [02/03] Timer Counter

    ...Remove the extra code and it should work for you?

    It's a full, working example. The reason your code isn't working is probably because you set Timer1.Interval to 15000 instead of 1000 for each second it decrements. I can't say for sure, it's not in your example.

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,383

    Re: [02/03] Timer Counter

    Quote Originally Posted by sheba2
    kleinma: It still does not work. It could be that I am putting the code in the wrong places.
    maybe.. I wouldn't be able to tell you without seeing your code.

  7. #7
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,817

    Re: [02/03] Timer Counter

    Quote Originally Posted by sheba2
    I am trying to code a counter in my game so the player has 15 seconds to answer the question. I have tried many different codes but all of them do things I don't want, like it counts down but only shows the 0 when finished counting down. It doesn't show the countdown from 15 to 1 on the form.

    Here is my code:

    Dim counter As Integer = 0
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Me.uiCounterLabel.Text = counter
    counter = counter - 1
    If counter <= 0 Then
    Timer1.Enabled = False
    counter = 15
    End If
    'clear out question and answer
    uiQuestTextBox.Text = ""
    uiAnsLabel2.Text = ""
    'message to player that time has elapsed
    MessageBox.Show("Your time is up!")
    'go back to main form
    Main1.Show()
    Me.Hide()

    End Sub

    I must be doing something wrong and hope someone can assist.
    Hi,

    I should do it this way, set your Timer to 1000 and false;

    VB Code:
    1. Dim sec As Integer = 15
    2.  Label1.Text = "15"
    3.  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As    System.EventArgs) Handles Button1.Click
    4.         Timer1.Enabled = True
    5.  
    6.  
    7.     End Sub
    8.  
    9.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    10.  
    11.         sec -= 1
    12.         If sec > 0 Then
    13.  
    14.             Label1.Text = sec
    15.         Else
    16.             Timer1.Enabled = False
    17.             Label1.Text = "0"
    18.             MessageBox.Show("Your time is up!")
    19.            
    20.         End If
    21.  
    22.     End Sub

    Hope it helps,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    19

    [RESOLVED] [02/03] Timer Counter

    Your coding works great. Thanks for your help!

  9. #9
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,817

    Re: [RESOLVED] [02/03] Timer Counter

    Quote Originally Posted by sheba2
    Your coding works great. Thanks for your help!
    Hi,

    No problem, mark your thread as resolved.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    19

    Re: [02/03] Timer Counter

    How do I mark my thread as resolved?

  11. #11
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,817

    Re: [02/03] Timer Counter

    Quote Originally Posted by sheba2
    How do I mark my thread as resolved?
    Hi,

    Go to your first post and find "thread tools" open it and click "Mark thread as resolved"

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

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