Solved

memory and memory recall buttons for a calculator

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hide vba in gp 7 96
VB6 ListBox Question 4 44
Export Data to Different .csv Files 26 106
VBA - If Bookmark = "XXBOOKMARKXX" then 15 29
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

832 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