?
Solved

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

Posted on 2006-07-06
4
Medium Priority
?
341 Views
Last Modified: 2012-06-27
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
Comment
Question by:supercoqui
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
Shakti109 earned 2000 total points
ID: 17055162

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
 
LVL 8

Expert Comment

by:Shakti109
ID: 17055184

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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17055391
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
 

Author Comment

by:supercoqui
ID: 17061490
Thanks for all the great answers.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question