VB Calculator problem
I've a homework on Calculator and this is what I got so far.
Only problem is when I type 1.2 0.2 it just don't come up right
and when I change the btn0 to the 1 similar to btn1 when at the start I could type 000000000 which shldn't be right on a calculator can someone help me debug this problem?
(need to keep with this code and not other codding)
Public Class Form1
'variables to hold operands
Private Var1 As Double
Private Var2 As Double
'varible to hold temporary values
Private Temp As Double
'variable to hold operator type : , -, *, /
'used to detect whether the operator button is clicked for a few times
Private [Operator] As String
'true when operator button click, false oterwise
Private PendingOperator As Boolean
'true when a dot is press else is false
Private DotOperator As Boolean
Public Sub Calculate()
Var2 = CDbl(txt.Text)
If [Operator] = "Add" Then
Var1 = Var1 Var2
ElseIf [Operator] = "Subtract" Then
Var1 = Var1 - Var2
ElseIf [Operator] = "Multiply" Then
Var1 = Var1 * Var2
ElseIf [Operator] = "Divide" Then
Var1 = Var1 / Var2
End If
text.Text = CStr(Var1)
PendingOperator = True
End Sub
'Btn 0-9 with exception of 0 all other button carries the same code
Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
If PendingOperator = False Then
If text.Text.Length > 0 Then
text.Text = text.Text CStr(0)
End If
End If
End Sub
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
If PendingOperator = False Then
text.Text = text.Text CStr(1)
Else
text.Text = 1
PendingOperator = False
End If
End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
If PendingOperator = False Then
text.Text = text.Text CStr(2)
Else
text.Text = 2
PendingOperator = False
End If
End Sub
Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
If PendingOperator = False Then
text.Text = text.Text CStr(3)
Else
text.Text = 3
PendingOperator = False
End If
End Sub
Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
If PendingOperator = False Then
text.Text = text.Text CStr(4)
Else
text.Text = 4
PendingOperator = False
End If
End Sub
Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
If PendingOperator = False Then
text.Text = text.Text CStr(5)
Else
text.Text = 5
PendingOperator = False
End If
End Sub
Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
If PendingOperator = False Then
text.Text = text.Text CStr(6)
Else
text.Text = 6
PendingOperator = False
End If
End Sub
Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
If PendingOperator = False Then
text.Text = text.Text CStr(7)
Else
text.Text = 7
PendingOperator = False
End If
End Sub
Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
If PendingOperator = False Then
text.Text = text.Text CStr(8)
Else
text.Text = 8
PendingOperator = False
End If
End Sub
Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
If PendingOperator = False Then
text.Text = text.Text CStr(9)
Else
text.Text = 9
PendingOperator = False
End If
End Sub
'cancel button
Private Sub btnce_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnce.Click
text.Clear()
Var1 = 0
Var2 = 0
[Operator] = ""
DotOperator = False
End Sub
'percentage button
Private Sub btnper_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnper.Click
If text.Text.Length <> 0 Then
Temp = CDbl(txt.Text)
Temp = (Temp / 100)
text.Text = CStr(Temp)
DotOperator = False
End If
End Sub
'squareroot button
Private Sub btnsqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsqrt.Click
If text.Text.Length <> 0 Then
Temp = CDbl(txt.Text)
Temp = System.Math.Sqrt(Temp)
text.Text = CStr(Temp)
DotOperator = False
End If
End Sub
'btn ,-,x,/ all carries the same code
Private Sub btndiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndiv.Click
If text.Text.Length <> 0 Then
If [Operator] = "" Then
'first time clicking
Var1 = CDbl(txt.Text)
PendingOperator = True
Else
'click a few times
Calculate()
End If
[Operator] = "Divide"
DotOperator = False
End If
End Sub
Private Sub btntime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntime.Click
If text.Text.Length <> 0 Then
If [Operator] = "" Then
'first time clicking x button
Var1 = CDbl(txt.Text)
PendingOperator = True
Else
'click a few times
Calculate()
End If
[Operator] = "Multiply"
DotOperator = False
End If
End Sub
Private Sub btnmin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmin.Click
If text.Text.Length <> 0 Then
If [Operator] = "" Then
'first time clicking - button
Var1 = CDbl(txt.Text)
PendingOperator = True
Else
'click a few times
Calculate()
End If
[Operator] = "Subtract"
DotOperator = False
End If
End Sub
Private Sub btnplu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnplu.Click
If text.Text.Length <> 0 Then
If [Operator] = "" Then
'first time clicking button
Var1 = CDbl(txt.Text)
PendingOperator = True
Else
'click a few times
Calculate()
End If
[Operator] = "Add"
DotOperator = False
End If
End Sub
'Decimal button
Private Sub btndec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndec.Click
If PendingOperator = False Then
If DotOperator = False Then
If text.Text.Length > 0 Then
text.Text = text.Text "."
Else
text.Text = "0."
End If
DotOperator = True
End If
End If
End Sub
'equal button
Private Sub btneq_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneq.Click
If text.Text.Length <> 0 AndAlso Var1 <> 0 Then
Calculate()
[Operator] = ""
DotOperator = False
End If
End Sub
End Class
|