Help with very basic adding

  • 13 years ago

    I am trying to mimic a windows calculator and am having problems with the running total.

    Working so far is when i click numbers they display in the text box

    but when i click add what i would like to do is clear the text box but store that number, then when the new number is entered is add that to the stored number to keep a running total.

    Then when the equals button is hit is displays that number in the running total....

     

    I know this may be basic to most but i need major help with this

  • 13 years ago

    Hi,

    This code is not mine but I don't remember from where have I got it, it is nice and simulates the windows calculator. To make it work, add the below controls to your form:

    • Label named "ReadOut".
    • 4 Buttons called Decimal, Percent, Cancel, and Cancel Entry
    • Array of 5 buttons called Operator (for + - / * =)
    • Array of 10 buttons called Number (for digits 0 to 9)

    And the code is:

    Option Explicit

    Dim Op1, Op2                ' Previously input operand.
    Dim DecimalFlag As Integer  ' Decimal point present yet?
    Dim NumOps As Integer       ' Number of operands.
    Dim LastInput               ' Indicate type of last keypress event.
    Dim OpFlag                  ' Indicate pending operation.
    Dim TempReadout




    ' Click event procedure for C (cancel) key.
    ' Reset the display and initializes variables.
    Private Sub Cancel_Click()
        Readout = Format(0, "0.")
        Op1 = 0
        Op2 = 0
        Form_Load
    End Sub






    Private Sub Cancel_KeyPress(KeyAscii As Integer)
    PressKey (KeyAscii)
    End Sub

    ' Click event procedure for CE (cancel entry) key.
    Private Sub CancelEntry_Click()
        Readout = Format(0, "0.")
        DecimalFlag = False
        LastInput = "CE"
    End Sub




    Private Sub CancelEntry_KeyPress(KeyAscii As Integer)
    PressKey (KeyAscii)
    End Sub


    Private Sub Command1_KeyPress(KeyAscii As Integer)
    PressKey (KeyAscii)
    End Sub



    Private Sub Command2_KeyPress(KeyAscii As Integer)
    PressKey (KeyAscii)
    End Sub


    ' Click event procedure for decimal point (.) key.
    ' If last keypress was an operator, initialize
    ' readout to "0." Otherwise, append a decimal
    ' point to the display.
    Private Sub Decimal_Click()
        If LastInput = "NEG" Then
            Readout = Format(0, "-0.")
        ElseIf LastInput <> "NUMS" Then
            Readout = Format(0, "0.")
        End If
        DecimalFlag = True
        LastInput = "NUMS"
    End Sub











    Private Sub Decimal_KeyPress(KeyAscii As Integer)
    PressKey (KeyAscii)
    End Sub


    ' Initialization routine for the form.
    ' Set all variables to initial values.
    Private Sub Form_Load()
        DecimalFlag = False
        NumOps = 0
        LastInput = "NONE"
        OpFlag = " "
        Readout = Format(0, "0.")
        'Decimal.Caption = Format(0, ".")
    End Sub









    ' Click event procedure for number keys (0-9).
    ' Append new number to the number in the display.
    Private Sub Number_Click(Index As Integer)
        If LastInput <> "NUMS" Then
            Readout = Format(0, ".")
            DecimalFlag = False
        End If
        If DecimalFlag Then
            Readout = Readout + Number(Index).Caption
        Else
            Readout = Left(Readout, InStr(Readout, Format(0, ".")) - 1) + Number(Index).Caption + Format(0, ".")
        End If
        If LastInput = "NEG" Then Readout = "-" & Readout
        LastInput = "NUMS"
    End Sub













    Private Sub Number_KeyPress(Index As Integer, KeyAscii As Integer)
    PressKey (KeyAscii)
    End Sub
    Sub PressKey(KeyAscii)
    Dim Char As String
    Char = Chr(KeyAscii)
        Select Case Char
        Case 1, 2, 3, 4, 5, 6, 7, 8, 9
            Number_Click (Char)
        Case "="
            Operator_Click (4)
        Case "+"
            Operator_Click (1)
        Case "-"
            Operator_Click (3)
        Case "/"
            Operator_Click (0)
        Case "*"
            Operator_Click (2)
        Case "%"
            Percent_Click
        Case "."
            Decimal_Click
        Case Else
            Exit Sub
        End Select
    End Sub

























    ' Click event procedure for operator keys (+, -, x, /, =).
    ' If the immediately preceeding keypress was part of a
    ' number, increments NumOps. If one operand is present,
    ' set Op1. If two are present, set Op1 equal to the
    ' result of the operation on Op1 and the current
    ' input string, and display the result.
    Private Sub Operator_Click(Index As Integer)
        TempReadout = Readout
        If LastInput = "NUMS" Then
            NumOps = NumOps + 1
        End If
        Select Case NumOps
            Case 0
            If Operator(Index).Caption = "-" And LastInput <> "NEG" Then
                Readout = "-" & Readout
                LastInput = "NEG"
            End If
            Case 1
            Op1 = Readout
            If Operator(Index).Caption = "-" And LastInput <> "NUMS" And OpFlag <> "=" Then
                Readout = "-"
                LastInput = "NEG"
            End If
            Case 2
            Op2 = TempReadout
            Select Case OpFlag
                Case "+"
                    Op1 = CDbl(Op1) + CDbl(Op2)
                Case "-"
                    Op1 = CDbl(Op1) - CDbl(Op2)
                Case "X"
                    Op1 = CDbl(Op1) * CDbl(Op2)
                Case "/"
                    If Op2 = 0 Then
                       MsgBox "Can't divide by zero", 48, "Calculator"
                    Else
                       Op1 = CDbl(Op1) / CDbl(Op2)
                    End If
                Case "="
                    Op1 = CDbl(Op2)
                Case "%"
                    Op1 = CDbl(Op1) * CDbl(Op2)
                End Select
            Readout = Op1
            NumOps = 1
        End Select
        If LastInput <> "NEG" Then
            LastInput = "OPS"
            OpFlag = Operator(Index).Caption
        End If
    End Sub

















































    Private Sub Operator_KeyPress(Index As Integer, KeyAscii As Integer)
    PressKey (KeyAscii)
    End Sub

    ' Click event procedure for percent key (%).
    ' Compute and display a percentage of the first operand.
    Private Sub Percent_Click()
        Readout = Readout / 100
        LastInput = "Ops"
        OpFlag = "%"
        NumOps = NumOps + 1
        DecimalFlag = True
    End Sub
    Private Sub Percent_KeyPress(KeyAscii As Integer)
        Dim Char As String
        Char = Chr(KeyAscii)
        If Char = "%" Then
            Readout = Readout / 100
            LastInput = "Ops"
            OpFlag = "%"
            NumOps = NumOps + 1
            DecimalFlag = True
        End If
    End Sub
    Public Sub Plus()
        CancelEntry.SetFocus
        SendKeys "{+}", True
    End Sub
    Public Sub Subtract()
        CancelEntry.SetFocus
        SendKeys "{-}", True
    End Sub
    Public Sub Divide()
        CancelEntry.SetFocus
        SendKeys "/", True
    End Sub
    Public Sub Equals()
        CancelEntry.SetFocus
        SendKeys "{=}", True
    End Sub
    Public Sub MultiPly()
        CancelEntry.SetFocus
        SendKeys "*", True
    End Sub
    Public Sub SendValue(Number As Double)
        CancelEntry.SetFocus
        SendKeys Number, True
    End Sub












































    Hope this works for you.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay