Timer Problem

Roberto

Member
Joined
Mar 9, 2006
Messages
16
Programming Experience
Beginner
Hi i can't work out how to solve this and am at my wits end with it. I'm experimenting with a times table tester that asks the user a question, waits 2 seconds then gives the answer and validates it against the user entered value. This i've got working, but i want to then clear all the boxes and repeat the process again so the user is asked ten questions in a row without having to click start again. Still being new to VB.net i've run out of ideas. Thanks in advance for any help on this.

VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1 = New Timer()
        Timer1.Interval = 2000

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'This happens after timer runs it's set interval

        'Validates user answer, txtResult box not visible to user
        txtResult.Text = Val(txtNumber1.Text) * Val(txtNumber2.Text)
        If txtResult.Text = txtUserResult.Text Then
            txtFeedback.Text = "CORRECT"
            txtFeedback.BackColor = Color.LightBlue
        Else
            txtFeedback.Text = "WRONG!"
            txtFeedback.BackColor = Color.Red
        End If
        If txtUserResult.Text = "" Then
            txtFeedback.Text = "OUT OF TIME"
            txtFeedback.BackColor = Color.Red
        End If

        Timer1.Stop()
        btnStart.Enabled = True

    End Sub

    Private Sub btnStart_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        'These events happen whilst timer1 runs

        Timer1.Start()

        ' Clears boxes
        txtResult.Text = ""
        txtFeedback.Text = ""
        txtFeedback.BackColor = Color.White
        txtUserResult.Text = ""
        txtNumber1.Text = ""
        txtNumber2.Text = ""

        'disable the start button
        btnStart.Enabled = False
        txtUserResult.Focus()

        'generate numbers
        If txtTitle.Text = "2 Times Table" Then
            txtNumber1.Text = (2)
            txtNumber2.Text = (Int(Rnd() * 12))
        End If

    End Sub
 
You'll need another timer. But start by moving this bit to a separate sub..

VB.NET:
 ' Clears boxes
        txtResult.Text = ""
        txtFeedback.Text = ""
        txtFeedback.BackColor = Color.White
        txtUserResult.Text = ""
        txtNumber1.Text = ""
        txtNumber2.Text = ""

        'disable the start button
        btnStart.Enabled = False
        txtUserResult.Focus()

        'generate numbers
        If txtTitle.Text = "2 Times Table" Then
            txtNumber1.Text = (2)
            txtNumber2.Text = (Int(Rnd() * 12))
        End If

Then call this sub from the btnstart event handler. Have your second interval set to 1000 and plonk a label on the form called labelcountdown or something. Add two class level variables if the type integer call one of them QuestionInterval and initialize it to 5 and the other TotalQuestionCount and initialize to 0. At the end of the timer1 tick event start the second timer and in the tick event put something like this...

VB.NET:
LabelCountDown.Text = QuestionInterval.ToString & " Seconds To Next Question"
 
QuestionInterval -=1
 
If QuestionInterval = 0 then
Timer2.enabled = false
'run the sub routine you moved all the code to
QuestionInterval = 5
LabelCountDown.Text = "QuestionInterval.ToString & " Seconds To Next Question"
'Or whatever you like 
Timer1.Enabled = True
End if


Then in the timer1 tick event increment the totalQuestioncount varaible by one after each question has been answered query it for it's value..

VB.NET:
If TotalQuestionCount = 10 then
'GameOver and do what ever
End if


Just knocked this up in my head so sorry for the lack of code, sounds ok upstairs. Give it a try and if you are having trouble following post back and we'll see how far you got and advise from there.
 
Followed your instructions this is what i did, no result with the code in action but i suspect i made a mistake instituting your code:

VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1 = New Timer()
        Timer1.Interval = 2000
        Timer2 = New Timer()
        Timer2.Interval = 1000
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'This happens after timer runs it's set interval

        Dim TotalQuestionCount As Integer = 0

        'Validates user answer, txtResult box not visible to user
        txtResult.Text = Val(txtNumber1.Text) * Val(txtNumber2.Text)
        If txtResult.Text = txtUserResult.Text Then
            txtFeedback.Text = "CORRECT"
            txtFeedback.BackColor = Color.LightBlue
        Else
            txtFeedback.Text = "WRONG!"
            txtFeedback.BackColor = Color.Red
        End If
        If txtUserResult.Text = "" Then
            txtFeedback.Text = "OUT OF TIME"
            txtFeedback.BackColor = Color.Red
        End If

        Timer1.Stop()
        btnStart.Enabled = False
        Timer2.Start()

        If TotalQuestionCount = 10 Then
            'GameOver and do what ever
        End If
    End Sub

    Private Sub btnStart_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        'These events happen whilst timer1 runs

        Timer1.Start()
    End Sub




    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim QuestionInterval As Integer = 5
        Dim TotalQuestionCount As Integer = 0
        lblCountdown.Text = QuestionInterval.ToString & " Seconds To Next Question"

        QuestionInterval -= 1

        If QuestionInterval = 0 Then
            Timer2.Enabled = False
            'run the sub routine you moved all the code to
            QuestionInterval = 5
lblCountdown.Text = "QuestionInterval.ToString & " Seconds To Next Question"
            'Or whatever you like 
            Timer1.Enabled = True
        End If
    End Sub

    

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        ' Clears boxes
        txtResult.Text = ""
        txtFeedback.Text = ""
        txtFeedback.BackColor = Color.White
        txtUserResult.Text = ""
        txtNumber1.Text = ""
        txtNumber2.Text = ""

        'disable the start button
        btnStart.Enabled = False
        txtUserResult.Focus()

        'generate numbers
        If txtTitle.Text = "2 Times Table" Then
            txtNumber1.Text = (2)
            txtNumber2.Text = (Int(Rnd() * 12))
        End If

    End Sub
 
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1 = New Timer()
        Timer1.Interval = 2000
        Timer2 = New Timer()
        Timer2.Interval = 1000
    End Sub

The above bit is ok.

Add the Following To Your Class.

VB.NET:
Private TotalQuestionCount As Integer = 0
Private QuestionInterval As Integer = 5
 
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'This happens after timer runs it's set interval
 
 
        'Validates user answer, txtResult box not visible to user
        txtResult.Text = Val(txtNumber1.Text) * Val(txtNumber2.Text)
        If txtResult.Text = txtUserResult.Text Then
            txtFeedback.Text = "CORRECT"
            txtFeedback.BackColor = Color.LightBlue
        Else
            txtFeedback.Text = "WRONG!"
            txtFeedback.BackColor = Color.Red
        End If
        If txtUserResult.Text = "" Then
            txtFeedback.Text = "OUT OF TIME"
            txtFeedback.BackColor = Color.Red
        End If
Timer1.Enabled = False
Me.TotalQuestionCount +=1
 
 If TotalQuestionCount = 10 Then
            MessageBox.Show("Game Over")
 
BtnStart.Enabled = True
Me.TotalQuestionCount = 0
Else
 
btnStart.Enabled = False
Timer2.Start()
End If
 
 
           End Sub

VB.NET:
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        'These events happen whilst timer1 runs
Me.StartGame
        Timer1.Start()
    End Sub


VB.NET:
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
 
lblCountdown.Text = QuestionInterval.ToString & " Seconds To Next Question"
 
QuestionInterval -= 1
 
If QuestionInterval = 0 Then
Timer2.Enabled = False
Me.StartGame
QuestionInterval = 5
lblCountdown.Text = "QuestionInterval.ToString & " Seconds To Next Question"
'Or whatever you like 
Timer1.Enabled = True
End If
End Sub
VB.NET:
Private Sub StartGame
 ' Clears boxes
        txtResult.Text = ""
        txtFeedback.Text = ""
        txtFeedback.BackColor = Color.White
        txtUserResult.Text = ""
        txtNumber1.Text = ""
        txtNumber2.Text = ""
 
        'disable the start button
        btnStart.Enabled = False
        txtUserResult.Focus()
 
        'generate numbers
        If txtTitle.Text = "2 Times Table" Then
            txtNumber1.Text = (2)
            txtNumber2.Text = (Int(Rnd() * 12))
        End If
End Sub


Try that and let me know how it goes. Looking again you probably won't need two timers actually. But if it works, what the hey.
 
Still no joy, i've entered all the code you said (i think) Getting an 'End Of Statement Expected' error in the Timer2 Tick Event highlighted in red, but otherwise okay.

VB.NET:
Public Class Form1
    Private TotalQuestionCount As Integer = 0
    Private QuestionInterval As Integer = 5
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1 = New Timer()
        Timer1.Interval = 2000
        Timer2 = New Timer()
        Timer2.Interval = 1000
 
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'This happens after timer runs it's set interval
 
        'Validates user answer, txtResult box not visible to user
        txtResult.Text = Val(txtNumber1.Text) * Val(txtNumber2.Text)
        If txtResult.Text = txtUserResult.Text Then
            txtFeedback.Text = "CORRECT"
            txtFeedback.BackColor = Color.LightBlue
        Else
            txtFeedback.Text = "WRONG!"
            txtFeedback.BackColor = Color.Red
        End If
        If txtUserResult.Text = "" Then
            txtFeedback.Text = "OUT OF TIME"
            txtFeedback.BackColor = Color.Red
        End If
        Timer1.Enabled = False
        Me.TotalQuestionCount += 1
 
        If TotalQuestionCount = 10 Then
            MessageBox.Show("Game Over")
 
            btnStart.Enabled = True
            Me.TotalQuestionCount = 10
        Else
 
            btnStart.Enabled = False
            Timer2.Start()
        End If
    End Sub
 
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        'These events happen whilst timer1 runs
        Me.StartGame()
        Timer1.Start()
    End Sub
 
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        lblCountdown.Text = QuestionInterval.ToString & " Seconds To Next Question"
 
        QuestionInterval -= 1
 
        If QuestionInterval = 0 Then
            Timer2.Enabled = False
            Me.StartGame()
            QuestionInterval = 5
lblCountdown.Text = QuestionInterval.ToString & " [COLOR=red]Seconds To Next Question[/COLOR]"
            'Or whatever you like 
            Timer1.Enabled = True
        End If
    End Sub
 
    Private Sub StartGame()
        ' Clears boxes
        txtResult.Text = ""
        txtFeedback.Text = ""
        txtFeedback.BackColor = Color.White
        txtUserResult.Text = ""
        txtNumber1.Text = ""
        txtNumber2.Text = ""
 
        'disable the start button
        btnStart.Enabled = False
        txtUserResult.Focus()
 
        'generate numbers
        If txtTitle.Text = "2 Times Table" Then
            txtNumber1.Text = (2)
            txtNumber2.Text = (Int(Rnd() * 12))
        End If
    End Sub
 
End Class
 
Ok, i've edited the your last post to reflect the changes. It was a oversight on my part, i put in some double quotes when i shouldn't have. Try it and let me know how it goes. Be advised though i haven't tested this code, so it's all from made up on the spot.
 
Back
Top