• 0

[VB .NET] Simple Question


Question

Hey guys, Simple Question, but for some stinking reason, I can't figure this out.

I am making a Math Quiz game, and need a timer. I have seperate levels, each changed by the click of a radio button.

What I want to do is, when the level is pressed, the timer should start, and end ticking in 6 seconds for easy, 4 seconds for medium, and 3 seconds for hard. After the timer is up, I want a message box saying So-and-so to pop up.

So, I know how to enable the timer and everything, but how do I code it so when the timer is up, call up a Message Box?

Oh, and if they answer the math problem correctly, the timer stops, and they get a message saying "Good job" and if they don't, "Oops, you didn't answer correctly" or, if the timer expires before they answer, "Too bad, times up!"

Can I have the code for this? For some reason I can't figure out how to do it... Thanks!

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

That's pretty easy... just make a integer variable as your counter for hard, medium and easy... and on every tick you increment it, and you just do a if condition to test if the counter is 6,4, or whatever then you display a message box.

Link to comment
Share on other sites

  • 0

Well here it is, it works but it's whipped up quickly so yeah...

Public Class Form1
 ? ?Inherits System.Windows.Forms.Form

 ? ?Private easyCounter As Integer
 ? ?Private mediumCounter As Integer
 ? ?Private hardCounter As Integer

 ? ?Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 ? ?End Sub

 ? ?Private Sub radioEasy_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radioEasy.CheckedChanged
 ? ? ? ?Me.tmrMain.Enabled = True
 ? ? ? ?Me.mediumCounter = 0
 ? ? ? ?Me.hardCounter = 0
 ? ?End Sub

 ? ?Private Sub tmrMain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMain.Tick

 ? ? ? ?If easyCounter = 6 And Me.radioEasy.Checked = True Then
 ? ? ? ? ? ?MessageBox.Show("Too bad, times up!", "Times Up", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
 ? ? ? ? ? ?easyCounter = 0
 ? ? ? ? ? ?Me.radioEasy.Checked = False
 ? ? ? ? ? ?Me.tmrMain.Enabled = False
 ? ? ? ?Else
 ? ? ? ? ? ?easyCounter += 1
 ? ? ? ?End If

 ? ? ? ?If mediumCounter = 4 And Me.radioMedium.Checked = True Then
 ? ? ? ? ? ?MessageBox.Show("Too bad, times up!", "Times Up", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
 ? ? ? ? ? ?mediumCounter = 0
 ? ? ? ? ? ?Me.radioMedium.Checked = False
 ? ? ? ? ? ?Me.tmrMain.Enabled = False
 ? ? ? ?Else
 ? ? ? ? ? ?mediumCounter += 1
 ? ? ? ?End If

 ? ? ? ?If hardCounter = 4 And Me.radioHard.Checked = True Then
 ? ? ? ? ? ?MessageBox.Show("Too bad, times up!", "Times Up", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
 ? ? ? ? ? ?hardCounter = 0
 ? ? ? ? ? ?Me.radioHard.Checked = False
 ? ? ? ? ? ?Me.tmrMain.Enabled = False
 ? ? ? ?Else
 ? ? ? ? ? ?hardCounter += 1
 ? ? ? ?End If

 ? ?End Sub

 ? ?Private Sub radioHard_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radioHard.CheckedChanged
 ? ? ? ?Me.tmrMain.Enabled = True
 ? ? ? ?Me.easyCounter = 0
 ? ? ? ?Me.mediumCounter = 0
 ? ?End Sub

 ? ?Private Sub radioMedium_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radioMedium.CheckedChanged
 ? ? ? ?Me.tmrMain.Enabled = True
 ? ? ? ?Me.easyCounter = 0
 ? ? ? ?Me.hardCounter = 0
 ? ?End Sub
End Class

here's the zip as well if you want.

Edited by Winston
Link to comment
Share on other sites

  • 0

Ok, I tried it, and still no message box popping up. Tell me whats wrong with my code! I can't stand it...

Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim Answer As Integer
    Private easycounter As Integer
    Private mediumcounter As Integer
    Private hardcounter As Integer
    Dim First As Integer
    Dim Second As Integer
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents tmrmain As System.Windows.Forms.Timer
    Friend WithEvents RadioEasy As System.Windows.Forms.RadioButton
    Friend WithEvents RadioMedium As System.Windows.Forms.RadioButton
    Friend WithEvents RadioHard As System.Windows.Forms.RadioButton
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.Label5 = New System.Windows.Forms.Label
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.RadioEasy = New System.Windows.Forms.RadioButton
        Me.RadioMedium = New System.Windows.Forms.RadioButton
        Me.RadioHard = New System.Windows.Forms.RadioButton
        Me.tmrmain = New System.Windows.Forms.Timer(Me.components)
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.ForeColor = System.Drawing.Color.Navy
        Me.Label1.Location = New System.Drawing.Point(8, 5)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(370, 35)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "MATH QUIZ"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label2
        '
        Me.Label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.Label2.Font = New System.Drawing.Font("Verdana", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.Location = New System.Drawing.Point(15, 95)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(90, 35)
        Me.Label2.TabIndex = 1
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label3
        '
        Me.Label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.Label3.Font = New System.Drawing.Font("Verdana", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label3.Location = New System.Drawing.Point(150, 95)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(90, 35)
        Me.Label3.TabIndex = 2
        Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label4
        '
        Me.Label4.AutoSize = True
        Me.Label4.Location = New System.Drawing.Point(120, 104)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(11, 16)
        Me.Label4.TabIndex = 3
        Me.Label4.Text = "+"
        '
        'Label5
        '
        Me.Label5.AutoSize = True
        Me.Label5.Location = New System.Drawing.Point(256, 104)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(11, 16)
        Me.Label5.TabIndex = 4
        Me.Label5.Text = "="
        '
        'TextBox1
        '
        Me.TextBox1.Font = New System.Drawing.Font("Verdana", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.TextBox1.Location = New System.Drawing.Point(280, 96)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(90, 37)
        Me.TextBox1.TabIndex = 5
        Me.TextBox1.Text = ""
        Me.TextBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        'Button1
        '
        Me.Button1.Font = New System.Drawing.Font("Myriad Condensed Web", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Button1.ForeColor = System.Drawing.Color.FromArgb(CType(56, Byte), CType(107, Byte), CType(5, Byte))
        Me.Button1.Location = New System.Drawing.Point(88, 192)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(208, 56)
        Me.Button1.TabIndex = 6
        Me.Button1.Text = "Answer"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(160, 256)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 7
        Me.Button2.Text = "Quit"
        '
        'RadioEasy
        '
        Me.RadioEasy.Location = New System.Drawing.Point(16, 192)
        Me.RadioEasy.Name = "RadioEasy"
        Me.RadioEasy.Size = New System.Drawing.Size(48, 24)
        Me.RadioEasy.TabIndex = 8
        Me.RadioEasy.Text = "Easy"
        '
        'RadioMedium
        '
        Me.RadioMedium.Location = New System.Drawing.Point(16, 216)
        Me.RadioMedium.Name = "RadioMedium"
        Me.RadioMedium.Size = New System.Drawing.Size(64, 24)
        Me.RadioMedium.TabIndex = 9
        Me.RadioMedium.Text = "Medium"
        '
        'RadioHard
        '
        Me.RadioHard.Location = New System.Drawing.Point(16, 240)
        Me.RadioHard.Name = "RadioHard"
        Me.RadioHard.Size = New System.Drawing.Size(48, 24)
        Me.RadioHard.TabIndex = 10
        Me.RadioHard.Text = "Hard"
        '
        'tmrmain
        '
        Me.tmrmain.Interval = 1000
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(385, 296)
        Me.Controls.Add(Me.RadioHard)
        Me.Controls.Add(Me.RadioMedium)
        Me.Controls.Add(Me.RadioEasy)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form1"
        Me.Text = "Math Quiz"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'seed the random number generator
        Randomize()

        'initialize the game
        CreateMathProblem()

        'enabled the "easy" level on startup
        RadioEasy.Checked = True



    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'quit button ends the progam, very simple
        End
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'make sure answer is a number
        If IsNumeric(TextBox1.Text) = False Then
            'display error message
            MsgBox("Please enter a number!")
        Else
            'Check the answer
            If Val(TextBox1.Text) = Answer Then
                MsgBox("You got it right! Good job!", , "Correct!")
            Else
                MsgBox("Woops, too bad, the answer was not correct.", , "Incorrect")
            End If
        End If
        'create a new math problem
        CreateMathProblem()

        'clear the answer field
        TextBox1.Text = ""

    End Sub

    Private Sub CreateMathProblem()
        

        'randomize the first field
        First = Int(Rnd() * 10)
        Label2.Text = First

        'randomize the second field
        Second = Int(Rnd() * 10)
        Label3.Text = Second

        'select the math operator and answer
        Select Case Int(Rnd() * 3)
            Case 0
                'addition problem
                Label4.Text = "+"
                Answer = First + Second
            Case 1
                'subtraction problem
                Label4.Text = "-"
                Answer = First - Second
            Case 2
                'multiplication problem
                Label4.Text = "x"
                Answer = First * Second
        End Select

    End Sub

    Private Sub radioEasy_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioEasy.CheckedChanged
        Me.tmrmain.Enabled = True
        Me.mediumcounter = 0
        Me.hardcounter = 0
    End Sub

    Private Sub tmrMain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrmain.Tick

        If easycounter = 6 And Me.RadioEasy.Checked = True Then
            MessageBox.Show("Too bad, times up!", "Times Up", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            easycounter = 0
            Me.RadioEasy.Checked = False
            Me.tmrmain.Enabled = False
        Else
            easycounter += 1
        End If

        If mediumcounter = 4 And Me.RadioMedium.Checked = True Then
            MessageBox.Show("Too bad, times up!", "Times Up", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            mediumcounter = 0
            Me.RadioMedium.Checked = False
            Me.tmrmain.Enabled = False
        Else
            mediumcounter += 1
        End If

        If hardcounter = 4 And Me.RadioHard.Checked = True Then
            MessageBox.Show("Too bad, times up!", "Times Up", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            hardcounter = 0
            Me.RadioHard.Checked = False
            Me.tmrmain.Enabled = False
        Else
            hardcounter += 1
        End If

    End Sub

    Private Sub radioHard_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioHard.CheckedChanged
        Me.tmrmain.Enabled = True
        Me.easycounter = 0
        Me.mediumcounter = 0
    End Sub

    Private Sub radioMedium_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioMedium.CheckedChanged
        Me.tmrmain.Enabled = True
        Me.easycounter = 0
        Me.hardcounter = 0
    End Sub
End Class

Help appreciated, thanks! :)

Link to comment
Share on other sites

  • 0

Bug:

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'seed the random number generator
        Randomize()

        'initialize the game
        CreateMathProblem()

        'enabled the "easy" level on startup
        RadioEasy.Checked = True

Change to:

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      'enabled the "easy" level on startup
        RadioEasy.Checked = True

        'seed the random number generator
        Randomize()

        'initialize the game
        CreateMathProblem()

You never actually check the option button on load the way you had it because it calls the subroutine "CreateMathProblem" first. So easy is not defaulted. I put it before anything else because its just an option for your GUI.

Fixed.

Edited by xStainDx
Link to comment
Share on other sites

  • 0

Thanks, that and a few other things fixed it. Now I have another question, though...

Sometimes, my second problem is more than my first problem ( ex.)

6 - 9, or 7- 21, etc

And this is not good in a subtraction problem, (duh) since the only way the program reads the inputted answer correct only when a negative integer. (If I say 3, the answer is incorrect, If I say -3, the answer is correct, and this is supposed to be for my sister and everything, you know...)

I've tried to make it so, If Label3.Text (2nd math problem) is greater than Label2.Text (first problem) then Label3.Text should subtract it's number by a few, and I've tried having them switch places if Label3.Text is greater than, and a bunch of other stuff.

It worked, I got no more problems with Label3.Text being greater than Label2.Text, BUT, it screws up my other math problems, like, if it was 2 + 2, I put in 4, it says it was incorrect, etc, and this happened with a few other problems. The source code is attached (does not have the attemps I used to try to fix the problem) if anyone wants to look at it or give me suggestions on how to make it work, it would be great!

Thanks all! :)

Math_Quiz__.zip

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.