Solved

memory and memory recall buttons for a calculator

Posted on 2000-02-22
4
1,062 Views
Last Modified: 2012-05-04
I'm very new to programming and to visual basic 6.  I have made a calculator program and the code is as follows:

Option Explicit

Dim bDecimal As Boolean     ' Declares variable to create a flag for the decimal
Dim Op1, Op2                ' Previously input operand.
Dim NumOps As Integer       ' Number of operands.
Dim LastInput               ' Indicate type of last keypress event.
Dim OpFlag                  ' Indicate pending operation.
Dim TempOutput              ' Temporary storage

' Click event procedure for number keys (0-9).
' Append new number to the number in the display.

Private Sub cmdNumber_Click(index As Integer)
    If LastInput <> "NUMS" Then
        txtOutput.Text = Format(0, ".")
        bDecimal = False
    End If
    If bDecimal Then
        txtOutput.Text = txtOutput.Text + cmdNumber(index).Caption
    Else
        txtOutput.Text = Left(txtOutput.Text, InStr(txtOutput.Text, Format(0, ".")) - 1) + cmdNumber(index).Caption + Format(0, ".")
    End If
    If LastInput = "NEG" Then txtOutput.Text = "-" & txtOutput.Text
    LastInput = "NUMS"
End Sub

' 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 cmdOperator_Click(index As Integer)
    TempOutput = txtOutput.Text
    If LastInput = "NUMS" Then
        NumOps = NumOps + 1
    End If
    Select Case NumOps
        Case 0
        If cmdOperator(index).Caption = "-" And LastInput <> "NEG" Then
            txtOutput.Text = "-" & txtOutput.Text
            LastInput = "NEG"
        End If
        Case 1
        Op1 = txtOutput.Text
        If cmdOperator(index).Caption = "-" And LastInput <> "NUMS" And OpFlag <> "=" Then
            txtOutput.Text = "-"
            LastInput = "NEG"
        End If
        Case 2
        Op2 = TempOutput
        Select Case OpFlag
            Case "+"
                Op1 = CDbl(Op1) + CDbl(Op2)
            Case "-"
                Op1 = CDbl(Op1) - CDbl(Op2)
            Case "*"
                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
        txtOutput.Text = Op1
        NumOps = 1
    End Select
    If LastInput <> "NEG" Then
        LastInput = "OPS"
        OpFlag = cmdOperator(index).Caption
    End If
End Sub

' Reset the display and initializes variables.

Private Sub cmdCancel_Click()
    txtOutput = Format(0, "0")
    Op1 = 0
    Op2 = 0
    Form_Load
End Sub

' Cancel last entry made

Private Sub cmdCancelEntry_Click()
    txtOutput.Text = Format(0, "0")
    bDecimal = False
    LastInput = "CE"
End Sub

' Will check to see if there is already a decimal
' if there is then the decimal is disabled

Private Sub cmdDecimal_Click()
If LastInput = "NEG" Then
        txtOutput.Text = Format(0, "-0.")
    ElseIf LastInput <> "NUMS" Then
        txtOutput.Text = Format(0, "0.")
    End If
    bDecimal = True
    LastInput = "NUMS"
End Sub

' Unload form

Private Sub cmdExit_Click()
End
End Sub

' Calculates the percent of txtOutput

Private Sub cmdPercent_Click()
    txtOutput.Text = txtOutput.Text / 100
    LastInput = "Ops"
    OpFlag = "%"
    NumOps = NumOps + 1
    bDecimal = True
End Sub

' Initialization routine for the form.
' Set all variables to initial values
' sets the flag to off when the form loads

Private Sub Form_Load()
bDecimal = False
NumOps = 0
LastInput = "NONE"
OpFlag = " "
txtOutput = Format(0, "0")
cmdDecimal.Caption = Format(0, ".")
End Sub

i need to have a memory button and a memory recall button.  Can someone please help me with the code for this.  Thanks in advance.


0
Comment
Question by:forbiddenR
  • 3
4 Comments
 
LVL 13

Expert Comment

by:crazyman
ID: 2549590
you could use the save setting and getsetting command.


in the save button use
savesetting app.exename,"settings","memory",YourValue


then in the recall button

YourValue=getsetting(app.exename,"settings","memory")
0
 

Author Comment

by:forbiddenR
ID: 2551043
The simpler the better if at all possible.  I haven't been taught how to use the savesetting/getsetting command yet.  So I would like to use something that I understand and that would fit easily into my code.  Please and thank you.
0
 
LVL 13

Accepted Solution

by:
crazyman earned 20 total points
ID: 2551143
If you wanted the value to remain in memory just as long as the calculator was running you could simply use a static variable to hold the current memory data and recall it when ever needed.


Private Sub SetMemory_Click()
    Memory "12345"
   
End Sub
Public Function Memory(Optional vNewVal As Variant) As Variant
    '''if a value is passed then the memory is changed
        ''' and new value returned
    '''otherwise the value is just returned
    Static vCurrentVal As Variant
    If Not IsMissing(vNewVal) Then vCurrentVal = vNewVal
    Memory = vCurrentVal
End Function

Private Sub GetMemory_Click()
    MsgBox Memory
End Sub


0
 
LVL 13

Expert Comment

by:crazyman
ID: 2551169
You could of course just use a public variable which is probably the easiest way.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question