11/12/2012

Mathematical Games for students or for Kids VB.Net code


In this post I am going to show how to designed and program a mathematical quiz program in VB.net.  This game is very simple and good entertainer for school students as well as kids and also it is  a brain twister adults. For this mathematical fun quiz you need to take following steps to designed and program it.
1.       Take a windows form and Add following on it
items
property
         Button
Btnfirst
Text – blank
Font-bold 14
         Button
         btnsecond
Text – blank
Font-bold 14
          Button
BtnPlus
Text   +
Font-bold 14
         Button
BtnNext
Text – Next
Font-bold 14
        TextBox
txtresult
Text – “”
Font-bold 14
         Label1
         Label2
         Label3
text-blank
To show the scores and time
                   Timer1-                            enable- time interval-1000


On placing all above components on your window form just write following vb.net code
Public Class FrmMath
    Dim time1 As Integer
    Dim intime As Integer = 0
   

    Private Sub FrmMath_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 1000
        Timer1.Enabled = True
        RandomNumber()
    End Sub

    Private Sub BtnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNext.Click
        Dim nu As Integer
        nu = (Val(BtnFirst.Text) + Val(Btnsecont.Text))
        If Val(txtresult.Text) = nu Then
           
            txtresult.Text = ""
            RandomNumber()
            count = count + 1 ‘ to count the number of right answer
            Label2.Text = "Right  - " & count
        Else
            txtresult.Text = ""
            RandomNumber()
            count1 = count1 + 1 ‘to count the number of wrong answer
            Label3.Text = "Wrong - " & count1
        End If
       
    End Sub

    Sub RandomNumber()
        'to generate RandomNumber
        Dim r(2) As Integer
        Dim random As New Random()
          
            r(1) = random.Next(99)
            r(2) = random.Next(99)
            BtnFirst.Text = r(1)
            Btnsecont.Text = r(2)
    End Sub

    Private Sub txtresult_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtresult.KeyPress
       ‘ to make the text box to accept only numberical value
 Dim validchar As String
        validchar = "0123456789."
        If Asc(e.KeyChar) >= 32 Then
            If InStr(validchar, e.KeyChar) = 0 Then
                e.Handled = True
            End If
        End If

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Start()
        intime = intime + 1
        low = True
        Label1.Text = "Total Time Elapsed  " & intime
    End Sub
   
End Class





Click here to get the code of advance version of same game in which various levels has been added to make it more entertaining and serious for adults.