Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a newbee. I have a windows form design with Visual Studio 2008 using Visual Basic. The form as 6 mathematical exercises. I will detail question1, but remember that there are 6 exercises.
I have the following:
label1.text then NumericUpDown1 then label2.text then a "=" sign then answer1.text
label1.text and label2.text are generated randomly.
Answer1 should equal the sum or the difference of label1.text and label2.text
The NumericUpDown contents are "+"(add) and "-"(subtract)

What I want to do is display the random numbers for label1.text and label2.text
I also want to determine whether it addition or subtraction.
What I am really having problems with is when the same number is generated by Random for label1.text and label2.text, Answer1 is results as incorrect.

Here is copy of what I have done so far.

VB
Public Class frmElSignoCorrecto
    Private mix As New Random
    Private add1, add2, add3, add4, add5, add6, add7, add8, add9, add10, add11, add12, add13, add14, add15, add16, add17, add18, add19, add20 As Integer
    Private timeLeft As Integer
    Private ans1, ans2, ans3, ans4, ans5, ans6, ans7, ans8, ans9, ans10 As Integer
    Private correct1, correct2, correct3, correct4, correct5, correct6, correct7, correct8, correct9, correct10 As String

    Private Sub frmElSignoCorrecto_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Add Items to ComboBoxes
        ComboBox1.Items.Add("+")
        ComboBox1.Items.Add("-")
        ComboBox2.Items.Add("+")
        ComboBox2.Items.Add("-")
        ComboBox3.Items.Add("+")
        ComboBox3.Items.Add("-")
        ComboBox4.Items.Add("+")
        ComboBox4.Items.Add("-")
        ComboBox5.Items.Add("+")
        ComboBox5.Items.Add("-")
        ComboBox6.Items.Add("+")
        ComboBox6.Items.Add("-")
        'Hide the checkmarks and X's
        picCkQ1.Visible = False
        picCkQ2.Visible = False
        picCkQ3.Visible = False
        picCkQ4.Visible = False
        picCkQ5.Visible = False
        picCk6.Visible = False
        picWrgQ1.Visible = False
        picWrgQ2.Visible = False
        picWrgQ3.Visible = False
        picWrgQ4.Visible = False
        picWrgQ5.Visible = False
        picWrg6.Visible = False
        'Hide Clock
        lblCountDown.Visible = False
        lblSegundos.Visible = False
        'Hide the Verificar Button
        btnVerificar.Visible = False
        btnStopTimerElSignoCorrecto.Visible = False
    End Sub

    Public Sub populateLabels()
        add1 = mix.Next(1, 7)
        add2 = mix.Next(1, 7)
        lblQ1P1.Text = add1.ToString
        lblQ1P2.Text = add2.ToString

        add3 = mix.Next(1, 7)
        add4 = mix.Next(1, 7)
        lblQ2P1.Text = add3.ToString
        lblQ2P2.Text = add4.ToString

        add5 = mix.Next(1, 7)
        add6 = mix.Next(1, 7)
        lblQ3P1.Text = add5.ToString
        lblQ3P2.Text = add6.ToString

        add7 = mix.Next(1, 7)
        add8 = mix.Next(1, 7)
        lblQ4P1.Text = add7.ToString
        lblQ4P2.Text = add8.ToString

        add9 = mix.Next(1, 7)
        add10 = mix.Next(1, 7)
        lblQ5P1.Text = add9.ToString
        lblQ5P2.Text = add10.ToString

        add11 = mix.Next(1, 7)
        add12 = mix.Next(1, 7)
        lblQ6P1.Text = add11.ToString
        lblQ6P2.Text = add12.ToString

    End Sub

    Private Sub addTheEquation()
        '....Equation 1
        If add1 < add2 Then
            ans1 = add1 + add2
            lblAns1.Text = ans1.ToString
            correct1 = "+"
        ElseIf add1 > add2 Then
            ans1 = add1 - add2
            lblAns1.Text = ans1.ToString
            correct1 = "-"
        ElseIf add1 = add2 Then
            ans1 = add1 + add2
            lblAns1.Text = ans1.ToString
            correct1 = "+"
        End If
        '.....Equation 2
        If add3 < add4 Then
            ans2 = add3 + add4
            lblAns2.Text = ans2.ToString
            correct2 = "+"
        ElseIf add3 > add4 Then
            ans2 = add3 - add4
            lblAns2.Text = ans2.ToString
            correct2 = "-"
        ElseIf add3 = add4 Then
            ans1 = add3 + add4
            lblAns2.Text = ans2.ToString
            correct2 = "+"
        End If
        '.....Equation 3 
        If add5 < add6 Then
            ans3 = add5 + add6
            lblAns3.Text = ans3.ToString
            correct3 = "+"
        ElseIf add5 > add6 Then
            ans3 = add5 - add6
            lblAns3.Text = ans3.ToString
            correct3 = "-"
        ElseIf add5 = add6 Then
            ans1 = add5 + add6
            lblAns3.Text = ans3.ToString
            correct3 = "+"
        End If
        If add7 < add8 Then
            ans4 = add7 + add8
            lblAns4.Text = ans4.ToString
            correct4 = "+"
        ElseIf add7 > add8 Then
            ans4 = add7 - add8
            lblAns4.Text = ans4.ToString
            correct4 = "-"
        ElseIf add7 = add8 Then
            ans1 = add7 + add8
            lblAns4.Text = ans4.ToString
            correct4 = "+"
        End If
        If add9 < add10 Then
            ans5 = add9 + add10
            lblAns5.Text = ans5.ToString
            correct5 = "+"
        ElseIf add9 > add10 Then
            ans5 = add9 - add10
            lblAns5.Text = ans5.ToString
            correct5 = "-"
        ElseIf add9 = add10 Then
            ans1 = add9 + add10
            lblAns5.Text = ans5.ToString
            correct5 = "+"
        End If
        If add11 < add12 Then
            ans6 = add11 + add12
            lblAns6.Text = ans6.ToString
            correct6 = "+"
        ElseIf add11 > add12 Then
            ans6 = add11 - add12
            lblAns6.Text = ans6.ToString
            correct6 = "-"
        ElseIf add11 = add12 Then
            ans1 = add11 + add12
            lblAns6.Text = ans6.ToString
            correct6 = "+"
        End If
    End Sub

    Public Function CheckTheAnswer()
        Dim text1 As String
        Dim text2 As String
        Dim text3 As String
        Dim text4 As String
        Dim text5 As String
        Dim text6 As String
        text1 = ComboBox1.SelectedText.ToString
        text2 = ComboBox2.SelectedText.ToString
        text3 = ComboBox4.SelectedText.ToString
        text4 = ComboBox4.SelectedText.ToString
        text5 = ComboBox5.SelectedText.ToString
        text6 = ComboBox6.SelectedText.ToString
        If text1 = correct1 AndAlso text2 = correct2 AndAlso text3 = correct3 AndAlso text4 = correct4 AndAlso text5 = correct5 AndAlso text6 = correct6 Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub HideTheMarksPics()
        picCkQ1.Visible = False
        picCkQ2.Visible = False
        picCkQ3.Visible = False
        picCkQ4.Visible = False
        picCkQ5.Visible = False
        picCk6.Visible = False
        picWrgQ1.Visible = False
        picWrgQ2.Visible = False
        picWrgQ3.Visible = False
        picWrgQ4.Visible = False
        picWrgQ5.Visible = False
        picCk6.Visible = False
    End Sub '

    Private Sub comboBoxClear()
        ComboBox1.Text = " "
        ComboBox2.Text = " "
        ComboBox3.Text = " "
        ComboBox4.Text = " "
        ComboBox5.Text = " "
        ComboBox6.Text = " "
    End Sub


    Private Sub picbxSinReloj_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picbxSinReloj.Click
        populateLabels()
        btnVerificar.Visible = True
        addTheEquation()
        HideTheMarksPics()
        comboBoxClear()
        btnStopTimerElSignoCorrecto.Visible = False
        btnVerificar.Visible = True
        'Stop the Timer
        TimerElSignoCorrecto.Stop()
        lblCountDown.Visible = False
        lblSegundos.Visible = False
        timeLeft = 0
    End Sub

    Private Sub picbxConReloj_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picbxConReloj.Click
        populateLabels()
        HideTheMarksPics()
        comboBoxClear()
        btnStopTimerElSignoCorrecto.Visible = True
        btnVerificar.Visible = False
        lblCountDown.Visible = True
        lblSegundos.Visible = True
        btnVerificar.Visible = False
        'Start the Timer
        TimerElSignoCorrecto.Start()
        lblCountDown.Text = "60"
        timeLeft = 60
    End Sub

    Private Sub btnVolver_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVolver.Click
        frmMenuOperadoresMixto.Show()
        Me.Hide()
    End Sub

    Private Sub TimerElSignoCorrecto_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerElSignoCorrecto.Tick
        If CheckTheAnswer() Then
            TimerElSignoCorrecto.Stop()
            picCkQ1.Visible = True
            picCkQ2.Visible = True
            picCkQ3.Visible = True
            picCkQ4.Visible = True
            picCkQ5.Visible = True
            picCk6.Visible = True
            MsgBox("Bien Hecho !!!", MsgBoxStyle.OkOnly)
        ElseIf timeLeft > 0 Then
            timeLeft -= 1
            lblCountDown.Text = timeLeft
        Else
            TimerElSignoCorrecto.Stop()
            MsgBox("Se Acabo el Tiempo", MsgBoxStyle.Critical)
        End If
    End Sub

    Private Sub btnVerificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerificar.Click



    End Sub



End Class
Posted
Updated 19-Sep-11 21:13pm
v3
Comments
Prerak Patel 20-Sep-11 0:23am    
Added code block.

1 solution

Here's your problem:
VB
ElseIf add11 = add12 Then
    ans1 = add11 + add12
    lblAns6.Text = ans6.ToString
    correct6 = "+"
End If
Change ans1 to ansN in all your ElseIf conditions - at the moment, when the inputs are the same, you always put the result into ans1 instead of ans2, or ans2, or...

But that is a fairly nasty way to do it! I would create a function which took three parameters, and which returned an answer - I would also change the condition to remove duplication.
VB
Private Function DoMath(ByRef operation As String, ByVal i As Integer, ByVal j As Integer) As Integer
       dim ans as Integer
       If i <= j Then
            ans = i + j
            operation = "+"
        Else
            ans = i - j
            operation = "-"
        End If
        Return ans
End Function
You can then use this repeatedly:
VB
ans1 = DoMath(correct1, add1, add2)
lblAns1.Text = ans1.ToString()

ans2 = DoMath(correct2, add3, add4)
lblAns2.Text = ans2.ToString()
This way, you only need to make changes in one place - there is no danger of forgetting one of the code blocks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900