I have been working on a progam to develop a simple Scientific Calculator. So far I’ve been doing ok, but now I am stuck in one area. In a calculator, once the = sign or button is pressed the caculator produces the results in the display. After the result is diplayed when you enter the next number, such as a “3”, for example, the result is cleared and the display shows the number 3.
Well, in my calculator after the = sign is pressed and the result is displayed, when I enter a new number it gets appended to the results. In other words, lets say that the result after pressing the = sign is 555, when I enter a new number, say 3, the diplay shows 3555 instead of clearing and displaying 3. The display does not clear unless I clich the Clear button.
How can make the new entry cancell the result and only show the new entry?
Below is my code for the calculator:
_________________________________________________________
Option Explicit On
Option Strict On
Imports System.Math
Public Class FrmSientificCal 'Variables that are availible throughout program
Dim dblDisplay As Double
Dim dblResult As Double
Dim strLastOperator As String
Dim strInput As String
Dim dblInput1 As Double
Dim dblInput2 As Double
Dim strMathOperant As String
Dim strDisplay As String
Private Sub subUpdateDisplay(ByVal strInput As String)
'This sub Procedure appends input. Input is appended in the order entered
If strLastOperator = "" Then
Me.txtDisplay.Text = Me.txtDisplay.Text & strInput
Else
Me.txtDisplay.Text = strInput
End If
strLastOperator = ""
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'When the program opens or loads, the display shows the string "0"
Me.txtDisplay.Text = "0"
strLastOperator = Me.txtDisplay.Text 'This replaces the string "0" with the first input
End Sub
Private Sub subAlert()
'This sub procedure does not allow operations on the load string "0" or the Clear string "0"
If Me.txtDisplay.Text = "0" Then
Beep()
End If
End Sub
Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
Call subUpdateDisplay("0")
End Sub
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
Call subUpdateDisplay("1")
End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
Call subUpdateDisplay("2")
End Sub
Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
Call subUpdateDisplay("3")
End Sub
Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
Call subUpdateDisplay("4")
End Sub
Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
Call subUpdateDisplay("5")
End Sub
Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
Call subUpdateDisplay("6")
End Sub
Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
Call subUpdateDisplay("7")
End Sub
Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
Call subUpdateDisplay("8")
End Sub
Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
Call subUpdateDisplay("9")
End Sub
Private Sub btnDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimal.Click
Call subUpdateDisplay("")
strDisplay = Me.txtDisplay.Text
If strDisplay.Contains(".") = True Then 'Does not allow more than one Decemal place
Beep()
Else
Me.txtDisplay.Text = strDisplay & "."
End If
End Sub
Private Sub btnPlusMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlusMinus.Click
Call subUpdateDisplay("")
strDisplay = Me.txtDisplay.Text
If strDisplay.Contains("-") = True Then 'Does not allow more than one Minus place
Beep()
Else
End If
If strDisplay.Contains("-") = False Then 'Convets input value to a Negative
Me.txtDisplay.Text = "-" & strDisplay
End If
Try
Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, Please enter numeric value"
Catch When strDisplay = "0"
Beep()
End Try
End Sub ' I was not able to make the +/- button toggle back to a positve value
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
'When the Clear button is clicked the diplay shows "0"
Me.txtDisplay.Text = "0"
strLastOperator = Me.txtDisplay.Text
End Sub
Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEqual.Click
'The Equal button determines which math function to operate based on a nested If...Then statement
Call subAlert()
Call subUpdateDisplay("")
Try
dblInput2 = Val(txtDisplay.Text)
If strMathOperant = "-" Then
txtDisplay.Text = CStr(dblInput1 - dblInput2)
ElseIf strMathOperant = "+" Then
txtDisplay.Text = CStr(dblInput1 + dblInput2)
dblInput1 = 0
ElseIf strMathOperant = "*" Then
txtDisplay.Text = CStr(dblInput1 * dblInput2)
ElseIf strMathOperant = "/" Then
txtDisplay.Text = CStr(dblInput1 / dblInput2)
End If
Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, Please enter numeric value"
End Try
End Sub
Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
'This is the functionality for the Devide button
Call subAlert()
Call subUpdateDisplay("")
Try
dblInput2 = Val(txtDisplay.Text)
strMathOperant = btnDivide.Text
dblInput1 = dblInput1 / dblInput2
dblInput1 = Val(txtDisplay.Text)
txtDisplay.Text = ""
Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR,Pleaes enter numeric value"
End Try
End Sub
Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
'This is the functionality for the Multiply button
Call subAlert()
Call subUpdateDisplay("")
Try
dblInput2 = Val(txtDisplay.Text)
strMathOperant = btnMultiply.Text
dblInput1 = dblInput1 * dblInput2
dblInput1 = Val(txtDisplay.Text)
txtDisplay.Text = ""
Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, Please numeric value"
End Try
End Sub
Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
'This is the functionality for the Subtract button
Call subAlert()
Call subUpdateDisplay("")
Try
dblInput2 = Val(txtDisplay.Text)
strMathOperant = btnSubtract.Text
dblInput1 = dblInput1 - dblInput2
dblInput1 = Val(txtDisplay.Text)
txtDisplay.Text = ""
Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, Please enter numeric value"
End Try
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
'This is the functionality for the Add button
Call subAlert()
Call subUpdateDisplay("")
Try
dblInput2 = Val(txtDisplay.Text)
strMathOperant = btnAdd.Text
dblInput1 = dblInput1 + dblInput2
txtDisplay.Text = ""
Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, please enter numeric value"
End Try
End Sub
Private Sub btnTangent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTangent.Click
'This is the functionality for the Tangent button
Call subAlert()
Try
strLastOperator = "Tan"
dblDisplay = CDbl(Me.txtDisplay.Text)
dblResult = Tan(dblDisplay)
Me.txtDisplay.Text = CStr(dblResult)
Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, Please enter numeric value"
End Try
End Sub
Private Sub btnSquareRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSquareRoot.Click
'This is the functionality for the SquareRoot button
Call subAlert()
Try
strLastOperator = "sqrt"
dblDisplay = CDbl(Me.txtDisplay.Text)
dblResult = Sqrt(dblDisplay)
Me.txtDisplay.Text = CStr(dblResult)
Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, Please enter numeric value"
End Try
End Sub
Private Sub btnSine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSine.Click
'This is the functionality for the Sine button
Call subAlert()
Try
strLastOperator = "Sin"
dblDisplay = CDbl(Me.txtDisplay.Text)
dblResult = Sin(dblDisplay)
Me.txtDisplay.Text = CStr(dblResult)
Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, Please enter numeric value"
End Try
End Sub
Private Sub btnCosine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCosine.Click
'This is the functionality for the Cosine button
Try
strLastOperator = "Cos"
dblDisplay = CDbl(Me.txtDisplay.Text)
dblResult = Cos(dblDisplay)
Me.txtDisplay.Text = CStr(dblResult)
Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, Please enter numeric value"
End Try
End Sub
Private Sub btnAbsValue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbsValue.Click
'This is the functionality for the Absolute Value button
Call subAlert()
Try
strLastOperator = "Abs"
dblDisplay = CDbl(Me.txtDisplay.Text)
dblResult = Abs(dblDisplay)
Me.txtDisplay.Text = CStr(dblResult)
Catch When Err.Number = 13 'Type mismatch
txtDisplay.Text = "ERROR, Please enter numeric value"
End Try
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
End 'Terminates the program
End Sub
End Class

New Topic/Question
Reply




MultiQuote





|