Click here to Skip to main content
15,914,165 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Stuck with the calculator project on the equals button, all codes are fine, but as I add more operations I do not get the answer instead I get Zero,scroll and:~ check cmdEquals Pliz help still an amatuer.

VB
Public Class frmCalculator
    Inherits System.Windows.Forms.Form
    Dim total1 As Single
    Dim total2 As Single
    Dim sub1, sub2 As Decimal
    Dim div1 As Integer
    Dim div2 As String
    Dim multiple1, multiple2 As Single
    Dim sign As String


    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSeven.Click
        'txtDisplay.Text = btnSeven.Text
        txtDisplay.Text = txtDisplay.Text & btnSeven.Text
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEight.Click
        'txtDisplay.Text = btnEight.Text
        txtDisplay.Text = txtDisplay.Text & btnEight.Text
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNine.Click
        'txtDisplay.Text = btnNine.Text
        txtDisplay.Text = txtDisplay.Text & btnNine.Text
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFour.Click
        'txtDisplay.Text = btnFour.Text
        txtDisplay.Text = txtDisplay.Text & btnFour.Text
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFive.Click
        'txtDisplay.Text = btnFive.Text
        txtDisplay.Text = txtDisplay.Text & btnFive.Text
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSix.Click
        'txtDisplay.Text = btnSix.Text
        txtDisplay.Text = txtDisplay.Text & btnSix.Text
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
        'txtDisplay.Text = btnOne.Text
        txtDisplay.Text = txtDisplay.Text & btnOne.Text
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTwo.Click
        'Dim MyOtherVariable As String
        'txtDisplay.Text = btnTwo.Text
        txtDisplay.Text = txtDisplay.Text & btnTwo.Text
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThree.Click
        'txtDisplay.Text = btnThree.Text
        txtDisplay.Text = txtDisplay.Text & btnThree.Text
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZero.Click
        'Dim MyVariable As String
        'txtDisplay.Text = btnZero.Text
        txtDisplay.Text = txtDisplay.Text & btnZero.Text
    End Sub

   
    Private Sub cmdSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMinus.Click
        sub1 = sub1 + Val(txtDisplay.Text)
        txtDisplay.Clear()
    End Sub

    Private Sub cmdEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEquals.Click
        'total2 = total1 + Val(txtDisplay.Text)
        'txtDisplay.Text = total2
        'total1 = 0
        'sub2 = sub1 - Val(txtDisplay.Text)
        'txtDisplay.Text = sub2
        'sub1 = 0
        'div2 = div1 / Val(txtDisplay.Text)
        'txtDisplay.Text = div2
        'div1 = 0
        multiple2 = multiple1 * Val(txtDisplay.Text)
        txtDisplay.Text = multiple2
        multiple1 = 0
    End Sub

    Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
        txtDisplay.Text = ""
    End Sub

    Private Sub txtDisplay_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDisplay.TextChanged

    End Sub

    Private Sub cmdPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPlus.Click
        total1 = total1 + Val(txtDisplay.Text)
        txtDisplay.Clear()

    End Sub


    Private Sub cmdDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDivide.Click
        div1 = div1 - Val(txtDisplay.Text)
        txtDisplay.Clear()

    End Sub

    Private Sub cmdMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMultiply.Click
        multiple1 = multiple1 + Val(txtDisplay.Text)
        txtDisplay.Clear()
    End Sub
End Class
Posted

1 solution

You really need to tidy up the code a bit! Button5_click handles Button7_Click, you are just going to confuse yourself!

When you perform an operation e.g. plus, minus, divide, multiply, i would set a flag somewhere to be used by the equal's operation to determine what to do with the calculation.

Think of the steps that could be performed on a real calculator;
Enter Value
Enter operation
Enter Value
Branch down either;
Enter Operation or Perform Equals

If it is then followed by a Number, then new calculation, if it is followed by an operation then move the Total back into sub-total and repeat above.

You then need to think of how you would use Total/Subtotal and move values back and forth, based on the operation/equals etc.
 
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