Solved

memory and memory recall buttons for a calculator

Posted on 2000-02-22
4
1,049 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now