trying to develop a simple calculator

gapaaboah

New member
Joined
Jun 16, 2011
Messages
1
Programming Experience
Beginner
I am new in programming, I am trying to develop a simple calculator but I am unable to connect the addition, subtraction and equal sign buttons. Please I need your help.This is the code I used

Public Class Form1

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 cmdEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEqual.Click
total2 = total1 + Val(txtDisplay.Text)
End Sub

Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click

End Sub

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

Private Sub btnTwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTwo.Click
txtDisplay.Text = btnTwo.Text
End Sub

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

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

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

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

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

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

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

Private Sub btnZero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZero.Click
txtDisplay.Text = btnZero.Text
End Sub
End Class
 
Please format code properly next time. Also some samples

Like some of the others say on this forum, if you make your code hard to read, it makes it more unlikly that someone will help you. Take a moment to see how to format the code on this forum. Use code blocks
HTML:
[CODE][/CODE]
and/or use
HTML:
[XCODE=vb][/XCODE]
. See the "Advanced" editor for posting, should be a link at the bottom near the post button if your not already using it. See http://www.vbdotnetforums.com/forum-feedback/14540-how-do-i-add-my-code-here.html

Here is your relevant code (i think)

    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 cmdEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEqual.Click
        total2 = total1 + Val(txtDisplay.Text)
    End Sub

What is your total1 variable? did you declare that elsewhere? without enough information, we have to guess,so i am guessing that what you wanted there was either a textbox.text or a variable you have declared elsewhere.

If it is a variable, a double, because you are using Val() method to convert the text into a double.

Here is one of your method rewritten, read the comments.

    Dim total1 As Double = 40D 'declared somewhere in your class level?.. if not declared then add this line

    Private Sub cmdPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPlus.Click

        Dim dblEntered As Double = 0D

        If Double.TryParse(txtDisplay.Text, dblEntered) Then 'this statement returns true if the tryparse method was able to convert the string into a double
            'it is passing dblEntered byRef, so if the statement returns true that means the converted value is passed into that variable
            total1 += dblEntered 'you can add that value here 'you can just use the += operator to add the value to an existing variable
            'total = total+ + dblEntered 'this is the same as the above, using different syntax
            'your going to want to display the result at some point? so assuming you have a label named lblDisplay, then to display the value
            lblDisplay.Text = total1.ToString()
        Else
            MsgBox("Please Enter a valid 'Double'. Ex: 56.34")
        End If

        txtDisplay.Clear()
    End Sub


To create a simple calculator all you need are the two variables you want to perform the operation to and something to display it in or output to.
Basically as shown in the code i posted, create a variable that is in class scope level to store the total in. Add/subtract to that value since i am assuming you want to keep a total.
so something like dim total as double = 0D
lets say for ex another variable: dim variableToAdd = 45D
so when you want to perform an operation to the total: total+= variableToAdd
if it was something different like multiplication you can do the same: total*= variableToAdd
and same for subtract and divide (note you cannot divide by 0 as you probably already know)

Then just display that value in the textbox like textBox.Text = total.ToString()

Here is a sample function to perform the calculations and return a string
    Enum MathOperator 'do not have to use ENUM (see alt. function below) i recommend a enum for this situation though
        add
        subtract
        multiply
        divide
    End Enum
    Function performMath(ByVal a As Double, ByVal typeOfOperator As MathOperator, ByVal b As Double) As String
        Dim result As Double = 0D

        Select Case typeOfOperator
            Case Is = MathOperator.add
                result = a + b
            Case Is = MathOperator.subtract
                result = a - b
            Case Is = MathOperator.multiply
                result = a * b
            Case Is = MathOperator.divide
                result = a / b
        End Select

        Return result.ToString()
    End Function
    'this is the same function, this does not use a ENUM type, it just uses as string. But for situations like this i highly recommend using an enumeration like shown above.
    Function performMath(ByVal a As Double, ByVal typeOfOperator As String, ByVal b As Double) As String
        Dim result As Double = 0D

        Select Case typeOfOperator
            Case Is = "add"
                result = a + b
            Case Is = "subtract"
                result = a - b
            Case Is = "multiply"
                result = a * b
            Case Is = "divide"
                result = a / b
        End Select

        Return result.ToString()
    End Function


Then to use it, something like..
    Private Sub cmdPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPlus.Click
        lblDisplay.Text = performMath(45, MathOperator.add, 56) 'note instead of Numbers you should be passing in variables
        'something like..
        lblDisplay.Text = performMath(dblFirstNumber, MathOperator.add, dblSecondNumber) 

        'or if you chose not to use the ENUM
        lblDisplay.Text = performMath(45, "add", 56)

        txtDisplay.Clear()
    End Sub

    'like this in your other operations you would just change the operator respectively
    Private Sub cmdPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPlus.Click
        lblDisplay.Text = performMath(45, MathOperator.subtract, 56) 'use variables instead

        txtDisplay.Clear()
    End Sub


and of course if you do not want to use a function you can just type it into the method instead, but using a method can help keep code neater.


Please expand your question with the relevant info and format the code properly if this does not answer your question. What do you mean by:
I am unable to connect the addition, subtraction and equal sign buttons.
 
Back
Top