Solved

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

Posted on 2006-07-06
4
328 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 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Programatically extract date from website 8 80
egit plugin on eclipse 8 82
Removing line numbers from left column in config file 7 61
ejb entity bean example 2 17
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

828 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