Solved

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

Posted on 2006-07-06
4
319 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
Comment Utility

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
Comment Utility

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
Comment Utility
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
Comment Utility
Thanks for all the great answers.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now