Ask Experts Questions for FREE Help !
Ask
    kakashi871's Avatar
    kakashi871 Posts: 1, Reputation: 1
    New Member
     
    #1

    Nov 8, 2011, 04:17 AM
    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

Check out some similar questions!

Vb 6.0 calculator [ 2 Answers ]

Please help me! On my prject , I need a source code of a vb 6.0 calculator please help. Tnx

Calculator [ 1 Answers ]

It could sum, product, divide, minus the number that input

How to create a calculator in VB 6.0 [ 1 Answers ]

How to create a calculator in VB 6.0

TI-84 Plus Calculator [ 1 Answers ]

This is sort of a math question and it isn't. I just bought this calculator yesterday, and the book that came with it does not tell me how to program the Quadratic formula as well as other equations into this calculator. And someone I know in my class has the same one and told me that this can be...

How to do e-0.05 on a calculator [ 5 Answers ]

Hi there, What are the exact steps I press on a calculator to do the following: - 40e-0.05 I keep getting the wrong answer? Any help is appreciated


View more questions Search
 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.