Timer Problem

Roberto

Member
Joined
Mar 9, 2006
Messages
16
Programming Experience
Beginner
i'm having trouble adding a timer to my program. It is a simple maths program with three TextBoxes. I would like the questions appearing in timed succession: 2 x 4 = 8 etc. The 2 when a 'Start' button is clicked, then a user selected length in seconds, then the same delay for the user to enter their answer before the 8 appears and is checked against their answer.
I have the rest of the code but cannot get my head around implementing a timer for it. Would appreciate any help for this.
 
Post some of your code and we can take a look at fitting the timer in.

mafro
 
All i've got so far are the workings of the buttons, end result will be started by clicking the button. Couldn't come up with a way to include a timer though:

VB.NET:
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
txtNumber1.Text = (Int(Rnd() * 10))
txtNumber2.Text = (Int(Rnd() * 10))
txtResult.Text = Val(txtNumber1.Text) * Val(txtNumber2.Text)
If txtResult.Text = txtUserResult.Text Then
txtFeedback.Text = "CORRECT"
txtFeedback.BackColor = Color.LightBlue
EndIf
If txtResult.Text <> txtUserResult.Text Then
txtFeedback.Text = "WRONG!"
txtFeedback.BackColor = Color.Red
EndIf
EndSub
 
Last edited by a moderator:
To clarify:
1) You generate 2 random numbers
2) Display them to the user and start a countdown timer
3) After the time runs out, show whether they go it right or wrong

Correct?

Ill put something together with your code there in the morning.
mafro
 
Rob, I created a form with the same form fields as you had in the code you posted and then added the timer.
This should be a good start for you, get back if you have any probs:

VB.NET:
Public Class Form1

    Private WithEvents timer As New Timer()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        timer = New Timer()
        timer.Interval = 8000        '8 seconds
    End Sub
    
    Private Sub timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer.Tick
        'this method will run 8 seconds after timer.Start()
        
        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
        
        'reset timer/button for next round
        timer.Stop()
        btnStart.Enabled = True
    End Sub
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        'generate numbers
        txtNumber1.Text = (Int(Rnd() * 10))
        txtNumber2.Text = (Int(Rnd() * 10))
        
        'start timer
        timer.Start()
        
        'disable the start button
        btnStart.Enabled = False
    End Sub
    
End Class
mafro
 
Back
Top