Solved

memory and memory recall buttons for a calculator

Posted on 2000-02-22
4
1,042 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You could of course just use a public variable which is probably the easiest way.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

743 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

17 Experts available now in Live!

Get 1:1 Help Now