home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Programmer'…arterly (Limited Edition) / Visual_Basic_Programmers_Journal_VB-CD_Quarterly_Limited_Edition_1995.iso / code / ch18code / calc.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1994-10-11  |  10.6 KB  |  351 lines

  1. VERSION 4.00
  2. Begin VB.Form Calculator 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Calculator"
  5.    ClientHeight    =   3000
  6.    ClientLeft      =   5385
  7.    ClientTop       =   1605
  8.    ClientWidth     =   3240
  9.    ClipControls    =   0   'False
  10.    BeginProperty Font 
  11.       name            =   "System"
  12.       charset         =   0
  13.       weight          =   700
  14.       size            =   9.75
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    Height          =   3405
  20.    Icon            =   "CALC.frx":0000
  21.    Left            =   5325
  22.    LinkMode        =   1  'Source
  23.    LinkTopic       =   "Form1"
  24.    MaxButton       =   0   'False
  25.    ScaleHeight     =   3000
  26.    ScaleWidth      =   3240
  27.    Top             =   1260
  28.    Width           =   3360
  29.    Begin VB.CommandButton Number 
  30.       Caption         =   "7"
  31.       Height          =   480
  32.       Index           =   7
  33.       Left            =   120
  34.       TabIndex        =   7
  35.       Top             =   600
  36.       Width           =   480
  37.    End
  38.    Begin VB.CommandButton Number 
  39.       Caption         =   "8"
  40.       Height          =   480
  41.       Index           =   8
  42.       Left            =   720
  43.       TabIndex        =   8
  44.       Top             =   600
  45.       Width           =   480
  46.    End
  47.    Begin VB.CommandButton Number 
  48.       Caption         =   "9"
  49.       Height          =   480
  50.       Index           =   9
  51.       Left            =   1320
  52.       TabIndex        =   9
  53.       Top             =   600
  54.       Width           =   480
  55.    End
  56.    Begin VB.CommandButton Cancel 
  57.       Caption         =   "C"
  58.       Height          =   480
  59.       Left            =   2040
  60.       TabIndex        =   10
  61.       Top             =   600
  62.       Width           =   480
  63.    End
  64.    Begin VB.CommandButton CancelEntry 
  65.       Caption         =   "CE"
  66.       Height          =   480
  67.       Left            =   2640
  68.       TabIndex        =   11
  69.       Top             =   600
  70.       Width           =   480
  71.    End
  72.    Begin VB.CommandButton Number 
  73.       Caption         =   "4"
  74.       Height          =   480
  75.       Index           =   4
  76.       Left            =   120
  77.       TabIndex        =   4
  78.       Top             =   1200
  79.       Width           =   480
  80.    End
  81.    Begin VB.CommandButton Number 
  82.       Caption         =   "5"
  83.       Height          =   480
  84.       Index           =   5
  85.       Left            =   720
  86.       TabIndex        =   5
  87.       Top             =   1200
  88.       Width           =   480
  89.    End
  90.    Begin VB.CommandButton Number 
  91.       Caption         =   "6"
  92.       Height          =   480
  93.       Index           =   6
  94.       Left            =   1320
  95.       TabIndex        =   6
  96.       Top             =   1200
  97.       Width           =   480
  98.    End
  99.    Begin VB.CommandButton Operator 
  100.       Caption         =   "+"
  101.       Height          =   480
  102.       Index           =   1
  103.       Left            =   2040
  104.       TabIndex        =   12
  105.       Top             =   1200
  106.       Width           =   480
  107.    End
  108.    Begin VB.CommandButton Operator 
  109.       Caption         =   "-"
  110.       Height          =   480
  111.       Index           =   3
  112.       Left            =   2640
  113.       TabIndex        =   13
  114.       Top             =   1200
  115.       Width           =   480
  116.    End
  117.    Begin VB.CommandButton Number 
  118.       Caption         =   "1"
  119.       Height          =   480
  120.       Index           =   1
  121.       Left            =   120
  122.       TabIndex        =   1
  123.       Top             =   1800
  124.       Width           =   480
  125.    End
  126.    Begin VB.CommandButton Number 
  127.       Caption         =   "2"
  128.       Height          =   480
  129.       Index           =   2
  130.       Left            =   720
  131.       TabIndex        =   2
  132.       Top             =   1800
  133.       Width           =   480
  134.    End
  135.    Begin VB.CommandButton Number 
  136.       Caption         =   "3"
  137.       Height          =   480
  138.       Index           =   3
  139.       Left            =   1320
  140.       TabIndex        =   3
  141.       Top             =   1800
  142.       Width           =   480
  143.    End
  144.    Begin VB.CommandButton Operator 
  145.       Caption         =   "X"
  146.       Height          =   480
  147.       Index           =   2
  148.       Left            =   2040
  149.       TabIndex        =   14
  150.       Top             =   1800
  151.       Width           =   480
  152.    End
  153.    Begin VB.CommandButton Operator 
  154.       Caption         =   "/"
  155.       Height          =   480
  156.       Index           =   0
  157.       Left            =   2640
  158.       TabIndex        =   15
  159.       Top             =   1800
  160.       Width           =   480
  161.    End
  162.    Begin VB.CommandButton Number 
  163.       Caption         =   "0"
  164.       Height          =   480
  165.       Index           =   0
  166.       Left            =   120
  167.       TabIndex        =   0
  168.       Top             =   2400
  169.       Width           =   1080
  170.    End
  171.    Begin VB.CommandButton Decimal 
  172.       Caption         =   "."
  173.       Height          =   480
  174.       Left            =   1320
  175.       TabIndex        =   18
  176.       Top             =   2400
  177.       Width           =   480
  178.    End
  179.    Begin VB.CommandButton Operator 
  180.       Caption         =   "="
  181.       Height          =   480
  182.       Index           =   4
  183.       Left            =   2040
  184.       TabIndex        =   16
  185.       Top             =   2400
  186.       Width           =   480
  187.    End
  188.    Begin VB.CommandButton Percent 
  189.       Caption         =   "%"
  190.       Height          =   480
  191.       Left            =   2640
  192.       TabIndex        =   17
  193.       Top             =   2400
  194.       Width           =   480
  195.    End
  196.    Begin VB.Label pInput 
  197.       Height          =   375
  198.       Left            =   480
  199.       TabIndex        =   20
  200.       Top             =   3360
  201.       Width           =   2055
  202.    End
  203.    Begin VB.Label Readout 
  204.       Alignment       =   1  'Right Justify
  205.       BackColor       =   &H0000FFFF&
  206.       BorderStyle     =   1  'Fixed Single
  207.       Caption         =   "0."
  208.       BeginProperty Font 
  209.          name            =   "MS Sans Serif"
  210.          charset         =   0
  211.          weight          =   700
  212.          size            =   12
  213.          underline       =   0   'False
  214.          italic          =   0   'False
  215.          strikethrough   =   0   'False
  216.       EndProperty
  217.       ForeColor       =   &H00000000&
  218.       Height          =   375
  219.       Left            =   120
  220.       TabIndex        =   19
  221.       Top             =   105
  222.       Width           =   3000
  223.    End
  224. Attribute VB_Name = "Calculator"
  225. Attribute VB_Creatable = False
  226. Attribute VB_Exposed = False
  227. ' ------------------------------------------------------------------------
  228. '               Copyright (C) 1994 Microsoft Corporation
  229. ' You have a royalty-free right to use, modify, reproduce and distribute
  230. ' the Sample Application Files (and/or any modified version) in any way
  231. ' you find useful, provided that you agree that Microsoft has no warranty,
  232. ' obligations or liability for any Sample Application Files.
  233. ' ------------------------------------------------------------------------
  234. Option Explicit
  235. Dim Op1, Op2                ' Previously input operand.
  236. Dim DecimalFlag As Integer  ' Decimal point present yet?
  237. Dim NumOps As Integer       ' Number of operands.
  238. Dim LastInput               ' Indicate type of last keypress event.
  239. Dim OpFlag                  ' Indicate pending operation.
  240. Dim TempReadout
  241. ' Click event procedure for C (cancel) key.
  242. ' Reset the display and initializes variables.
  243. Private Sub Cancel_Click()
  244.     Readout = "0."
  245.     Op1 = 0
  246.     Op2 = 0
  247.     Form_Load
  248. End Sub
  249. ' Click event procedure for CE (cancel entry) key.
  250. Private Sub CancelEntry_Click()
  251.     Readout = "0."
  252.     DecimalFlag = False
  253.     LastInput = "CE"
  254. End Sub
  255. ' Click event procedure for decimal point (.) key.
  256. ' If last keypress was an operator, initialize
  257. ' readout to "0." Otherwise, append a decimal
  258. ' point to the display.
  259. Public Sub Decimal_Click()
  260.     If LastInput = "NEG" Then
  261.         Readout = "-0."
  262.     ElseIf LastInput <> "NUMS" Then
  263.         Readout = "0."
  264.     End If
  265.     DecimalFlag = True
  266.     LastInput = "NUMS"
  267. End Sub
  268. ' Initialization routine for the form.
  269. ' Set all variables to initial values.
  270. Private Sub Form_Load()
  271.     DecimalFlag = False
  272.     NumOps = 0
  273.     LastInput = "NONE"
  274.     OpFlag = " "
  275. End Sub
  276. ' Click event procedure for number keys (0-9).
  277. ' Append new number to the number in the display.
  278. Public Sub Number_Click(index As Integer)
  279.     If LastInput <> "NUMS" Then
  280.         Readout = "."
  281.         DecimalFlag = False
  282.     End If
  283.     If DecimalFlag Then
  284.         Readout = Readout + Number(index).Caption
  285.     Else
  286.         Readout = Left(Readout, InStr(Readout, ".") - 1) + Number(index).Caption + "."
  287.     End If
  288.     If LastInput = "NEG" Then Readout = "-" & Readout
  289.     LastInput = "NUMS"
  290. End Sub
  291. ' Click event procedure for operator keys (+, -, x, /, =).
  292. ' If the immediately preceeding keypress was part of a
  293. ' number, increments NumOps. If one operand is present,
  294. ' set Op1. If two are present, set Op1 equal to the
  295. ' result of the operation on Op1 and the current
  296. ' input string, and display the result.
  297. Public Sub Operator_Click(index As Integer)
  298.     TempReadout = Readout
  299.     If LastInput = "NUMS" Then
  300.         NumOps = NumOps + 1
  301.     End If
  302.     Select Case NumOps
  303.         Case 0
  304.         If Operator(index).Caption = "-" And LastInput <> "NEG" Then
  305.             Readout = "-" & Readout
  306.             LastInput = "NEG"
  307.         End If
  308.         Case 1
  309.         Op1 = Readout
  310.         If Operator(index).Caption = "-" And LastInput <> "NUMS" And OpFlag <> "=" Then
  311.             Readout = "-"
  312.             LastInput = "NEG"
  313.         End If
  314.         Case 2
  315.         Op2 = TempReadout
  316.         Select Case OpFlag
  317.             Case "+"
  318.                 Op1 = Val(Op1) + Val(Op2)
  319.             Case "-"
  320.                 Op1 = Op1 - Op2
  321.             Case "X"
  322.                 Op1 = Op1 * Op2
  323.             Case "/"
  324.                 If Op2 = 0 Then
  325.                    MsgBox "Can't divide by zero", 48, "Calculator"
  326.                 Else
  327.                    Op1 = Op1 / Op2
  328.                 End If
  329.             Case "="
  330.                 Op1 = Op2
  331.             Case "%"
  332.                 Op1 = Op1 * Op2
  333.             End Select
  334.         Readout = Op1
  335.         NumOps = 1
  336.     End Select
  337.     If LastInput <> "NEG" Then
  338.         LastInput = "OPS"
  339.         OpFlag = Operator(index).Caption
  340.     End If
  341. End Sub
  342. ' Click event procedure for percent key (%).
  343. ' Compute and display a percentage of the first operand.
  344. Public Sub Percent_Click()
  345.     Readout = Readout / 100
  346.     LastInput = "Ops"
  347.     OpFlag = "%"
  348.     NumOps = NumOps + 1
  349.     DecimalFlag = True
  350. End Sub
  351.