• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

How to add a minus button to calculator app in VB 2005

Ok I am making a calculator program, it works fine for calculating Cos, tan and other things, now i am trying to make it work like a normal calculator when doing sum, division, multiplication, and substraction. I tried to make the substraction button first but it is giving me a exception. How can i code this substraction button?

Here is the code so far so everyone can see how the program works, the substraction button is the last entry. It gives me a "Conversion from string "" to type 'Double' is not valid."
 

Public Class Form1

    Dim enteredValue As String
    Dim Result As Double
    Dim roundedNumber As Double
    Dim reply
    Dim Value1 As Double
    Dim Value2 As Double


    'assigns a variable to the textbox value.
    Private Sub txtDisplay_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDisplay.TextChanged
        enteredValue = txtDisplay.Text
    End Sub



    'defines what the 0 button does when clicked. It adds a 0 to the display text box.
    Private Sub Btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn0.Click
        txtDisplay.Text = txtDisplay.Text & "0"
    End Sub

    'defines what the 1 button does when clicked. It adds a 1 to the display button.
    Private Sub Btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn1.Click
        txtDisplay.Text = txtDisplay.Text & "1"
    End Sub

    'defines what the 2 button does when clicked. It adds a 1 to the display button.
    Private Sub Btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn2.Click
        txtDisplay.Text = txtDisplay.Text & "2"
    End Sub

    'defines what the 3 button does when clicked. It adds a 1 to the display button.
    Private Sub Btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn3.Click
        txtDisplay.Text = txtDisplay.Text & "3"
    End Sub

    'defines what the 4 button does when clicked. It adds a 1 to the display button.
    Private Sub Btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn4.Click
        txtDisplay.Text = txtDisplay.Text & "4"
    End Sub

    'defines what the 5 button does when clicked. It adds a 1 to the display button.
    Private Sub Btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn5.Click
        txtDisplay.Text = txtDisplay.Text & "5"
    End Sub

    'defines what the 6 button does when clicked. It adds a 1 to the display button.
    Private Sub Btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn6.Click
        txtDisplay.Text = txtDisplay.Text & "6"
    End Sub

    'defines what the 7 button does when clicked. It adds a 1 to the display button.
    Private Sub Btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn7.Click
        txtDisplay.Text = txtDisplay.Text & "7"
    End Sub

    'defines what the 8 button does when clicked. It adds a 1 to the display button.
    Private Sub Btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn8.Click
        txtDisplay.Text = txtDisplay.Text & "8"
    End Sub

    'defines what the 9 button does when clicked. It adds a 1 to the display button.
    Private Sub Btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn9.Click
        txtDisplay.Text = txtDisplay.Text & "9"
    End Sub

    'defines what happens when the . button is pressed.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        txtDisplay.Text = txtDisplay.Text & "."
    End Sub

   


    'Defines what the Square button does when clicked.
    Private Sub BtnAbs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAbs.Click
        Try
            Result = Math.Abs(CDbl(txtDisplay.Text))

            'Command to catch exceptions and handle them.
        Catch ex As Exception
            MsgBox("There was an error in the calculation" & vbCrLf & "The value entered will be erased. Please enter a different value", , "Calculator Pro Version 1.0")
            txtDisplay.Text = ""

        End Try
    End Sub


    'Defines what the Square button does when clicked.
    Private Sub BtnSquare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSquare.Click
        Try
            Result = Math.Sqrt(CDbl(txtDisplay.Text))

            'Command to catch exceptions and handle them.
        Catch ex As Exception
            MsgBox("There was an error in the calculation" & vbCrLf & "The value entered will be erased. Please enter a different value", , "Calculator Pro Version 1.0")
            txtDisplay.Text = ""

        End Try
    End Sub

    'Defines what the Sin button does when clicked.
    Private Sub BtnSin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSin.Click
        Try
            Result = Math.Sin(CDbl(txtDisplay.Text))

            'Command to catch exceptions and handle them.
        Catch ex As Exception
            MsgBox("There was an error in the calculation" & vbCrLf & "The value entered will be erased. Please enter a different value", , "Calculator Pro Version 1.0")
            txtDisplay.Text = ""

        End Try
    End Sub

    'Defines what happens when the Cos button is clicked.
    Private Sub BtnCos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCos.Click
        Try
            Result = Math.Cos(CDbl(txtDisplay.Text))

            'Command to catch exceptions and handle them.
        Catch ex As Exception
            MsgBox("There was an error in the calculation" & vbCrLf & "The value entered will be erased. Please enter a different value", , "Calculator Pro Version 1.0")
            txtDisplay.Text = ""

        End Try
    End Sub

    'defines what happens when the Tan button is clicked.
    Private Sub BtnTan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnTan.Click
        Try
            Result = Math.Tan(CDbl(txtDisplay.Text))

            'Command to catch exceptions and handle them.
        Catch ex As Exception
            MsgBox("There was an error in the calculation" & vbCrLf & "The value entered will be erased. Please enter a different value", , "Calculator Pro Version 1.0")
            txtDisplay.Text = ""

        End Try
    End Sub





    'defines what happens when the calculate button is clicked.
    Private Sub CalculateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateBtn.Click
        txtDisplay.Text = Result
    End Sub




    'defines what happens when the Round value button is clicked. Rounds the value on the textbox.
    Private Sub RNDBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RNDBtn.Click
        Try
            roundedNumber = Math.Round(CDbl(txtDisplay.Text))
            txtDisplay.Text = roundedNumber

            'Command to catch exceptions and handle them.
        Catch ex As Exception
            MsgBox("There was an error in the calculation" & vbCrLf & "The value entered will be erased. Please enter a different value", , "Calculator Pro Version 1.0")
            txtDisplay.Text = ""

        End Try
    End Sub




    'defines what happens when the Clear button is clicked.
    Private Sub ClearBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearBtn.Click
        txtDisplay.Clear()
    End Sub

    'Defines what the exit button does when clicked (it opens a box to confirm closure).
    Private Sub ExitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitBtn.Click
        reply = MsgBox("Are you sure you want to close this application?", vbYesNo, "Closing Calculator Pro Version 1.0")
        If reply = vbYes Then Close()
    End Sub





    'defines what happens when the - button is pressed. The first value is assigned to a variable then it waits for the next input and calculates displays the value when the calcualte button is pressed.
    Private Sub minusBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles minusBtn.Click
        Value1 = txtDisplay.Text
        txtDisplay.Clear()
        Value2 = txtDisplay.Text
        Result = Value1 - Value2
    End Sub
End Class
0
supercoqui
Asked:
supercoqui
  • 2
1 Solution
 
Shakti109Commented:

For arithmetic operations, I would add an "operator" flag, and then you can keep your "calculate" routine somewhat central.

For example :

    'defines what happens when the - button is pressed. Converts the first number into a double value, clears the display and "waits" for the next input. Wen calculate is pressed, the appropriate action is taken.
    Private Sub minusBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles minusBtn.Click
        Operation = "Subtract"
        Value1 = cdbl(txtDisplay.Text)
        txtDisplay.Clear()
    End Sub
End Class


    'defines what happens when the calculate button is clicked.
    Private Sub CalculateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateBtn.Click
       
        if operation = "Subtract" then
           txtdisplay.text = value1 - cdbl(txtdisplay.text)
        else if operation = "addition" then
           txtdisplay.text = value1 + cdbl(txtdisplay.text)
        else if opertaion = "multiply" then
           ...etc etc..
        end if
       
       value1 = ""
       operation = ""
    End Sub
0
 
Shakti109Commented:

Note : You will want to define "value1" and "operation" globally to the form (or routine in which it is used) so it can be used by all routines.

I. E :  
   Dim value1 As Double
   Dim operation As String

0
 
Bob LearnedCommented:
Instead of CDbl or any of the conversion functions, use the Val() function, because it is very forgiving and won't give you any exceptions with an empty string.

Bob
0
 
supercoquiAuthor Commented:
Thanks for all the great answers.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now