HI Everyone,
I've built this (very rudimentary) calculator and can't to do more than one operation at a time (5 +5 =10, 5+5+5=10) as it's only holding the last two numbers entered. I've tried using
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Number1 = Number1 + Val(txtDisplay.Text)
txtDisplay.Text = ""
txtDisplay.Focus()
User = "+"
End Sub
But doesn't help, also tried quite a few other things. I haven't been able to find an answer and was hoping that someone could throw me a hint on this.
Here's most of the code:
Dim Number1 As Double
Dim Number2 As Double
Dim Numberx As Double
Dim conversion As Double
Dim User As String
Dim hasDecimal As Boolean
Dim tmpValue As Double
Private Sub frmCalculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub buttons(sender As Object, e As EventArgs) Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click
If txtDisplay.Text = "" Then txtDisplay.Text = sender.text Else txtDisplay.Text = txtDisplay.Text & sender.text
txtDisplay.Focus()
End Sub
Private Sub btnCE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCE.Click
txtDisplay.Text = ""
txtDisplay.Focus()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtDisplay.Text = String.Empty : Number1 = 0.0 : Number2 = 0.0
txtDisplay.Focus()
End Sub
Private Sub btnDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimal.Click
If InStr(txtDisplay.Text, ".") > 0 Then
Exit Sub
Else
txtDisplay.Text = txtDisplay.Text & "."
End If
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Number1 = Val(txtDisplay.Text)
txtDisplay.Text = ""
txtDisplay.Focus()
User = "+"
End Sub
Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
Number1 = Val(txtDisplay.Text)
txtDisplay.Text = ""
txtDisplay.Focus()
User = "-"
End Sub
Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
Number1 = Val(txtDisplay.Text)
txtDisplay.Text = ""
txtDisplay.Focus()
User = "*"
End Sub
Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
Number1 = Val(txtDisplay.Text)
txtDisplay.Text = ""
txtDisplay.Focus()
User = "/"
End Sub
Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
Dim Result As Double
Number2 = txtDisplay.Text
Select Case User
Case "+"
Result = Number1 + Number2
txtDisplay.Text = Result.ToString()
Case "-"
Result = Number1 - Number2
txtDisplay.Text = Result.ToString()
Case "/"
Result = Number1 / Number2
txtDisplay.Text = Result.ToString()
Case "*"
Result = Number1 * Number2
txtDisplay.Text = Result.ToString()
Case "x^"
Result = Number1 ^ Number2
txtDisplay.Text = Result.ToString()
Case "1/x"
Result = Number1 * 1 / 100
txtDisplay.Text = Result.ToString()
Case "PosNeg"
Result = Number1 * -1
txtDisplay.Text = Result.ToString()
End Select
txtDisplay.Text = Result.ToString()
End Sub
I know that it's pretty Frankensteined up, but it's what I was able to come up with that works, Well, except for the fact that it won't perform more than one op at a time.
___________________________________________________________
Actually, using:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Number1 = Number1 + Val(txtDisplay.Text)
txtDisplay.Text = ""
txtDisplay.Focus()
User = "+"
Works for the addition (5+6+9+4=24), but I can't figure out to handle the subtraction. I've put in many different types of
Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
num1 - Val(txtDisplay.Text)
( +, -, +-, etc.) all give me a wrong result. Any help at all would be much appreciated!!