home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / ncalc / ncalc.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-05-08  |  16.7 KB  |  535 lines

  1. VERSION 2.00
  2. Begin Form Calculator 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Calculator"
  5.    ClientHeight    =   2985
  6.    ClientLeft      =   1215
  7.    ClientTop       =   1695
  8.    ClientWidth     =   3240
  9.    FontBold        =   -1  'True
  10.    FontItalic      =   0   'False
  11.    FontName        =   "System"
  12.    FontSize        =   9.75
  13.    FontStrikethru  =   0   'False
  14.    FontUnderline   =   0   'False
  15.    Height          =   3390
  16.    Icon            =   NCALC.FRX:0000
  17.    Left            =   1155
  18.    LinkMode        =   1  'Source
  19.    LinkTopic       =   "Form1"
  20.    MaxButton       =   0   'False
  21.    ScaleHeight     =   2985
  22.    ScaleWidth      =   3240
  23.    Top             =   1350
  24.    Width           =   3360
  25.    Begin CommandButton Percent 
  26.       Caption         =   "%"
  27.       FontBold        =   -1  'True
  28.       FontItalic      =   0   'False
  29.       FontName        =   "System"
  30.       FontSize        =   9.75
  31.       FontStrikethru  =   0   'False
  32.       FontUnderline   =   0   'False
  33.       Height          =   480
  34.       Left            =   2640
  35.       TabIndex        =   17
  36.       Top             =   2400
  37.       Width           =   480
  38.    End
  39.    Begin CommandButton Operator 
  40.       Caption         =   "="
  41.       FontBold        =   -1  'True
  42.       FontItalic      =   0   'False
  43.       FontName        =   "System"
  44.       FontSize        =   9.75
  45.       FontStrikethru  =   0   'False
  46.       FontUnderline   =   0   'False
  47.       Height          =   480
  48.       Index           =   4
  49.       Left            =   2040
  50.       TabIndex        =   16
  51.       Top             =   2400
  52.       Width           =   480
  53.    End
  54.    Begin CommandButton Decimal 
  55.       Caption         =   "."
  56.       FontBold        =   -1  'True
  57.       FontItalic      =   0   'False
  58.       FontName        =   "System"
  59.       FontSize        =   9.75
  60.       FontStrikethru  =   0   'False
  61.       FontUnderline   =   0   'False
  62.       Height          =   480
  63.       Left            =   1320
  64.       TabIndex        =   18
  65.       Top             =   2400
  66.       Width           =   480
  67.    End
  68.    Begin CommandButton Number 
  69.       Caption         =   "0"
  70.       FontBold        =   -1  'True
  71.       FontItalic      =   0   'False
  72.       FontName        =   "System"
  73.       FontSize        =   9.75
  74.       FontStrikethru  =   0   'False
  75.       FontUnderline   =   0   'False
  76.       Height          =   480
  77.       Index           =   0
  78.       Left            =   120
  79.       TabIndex        =   0
  80.       Top             =   2400
  81.       Width           =   1080
  82.    End
  83.    Begin CommandButton Operator 
  84.       Caption         =   "/"
  85.       FontBold        =   -1  'True
  86.       FontItalic      =   0   'False
  87.       FontName        =   "System"
  88.       FontSize        =   9.75
  89.       FontStrikethru  =   0   'False
  90.       FontUnderline   =   0   'False
  91.       Height          =   480
  92.       Index           =   0
  93.       Left            =   2640
  94.       TabIndex        =   15
  95.       Top             =   1800
  96.       Width           =   480
  97.    End
  98.    Begin CommandButton Operator 
  99.       Caption         =   "X"
  100.       FontBold        =   -1  'True
  101.       FontItalic      =   0   'False
  102.       FontName        =   "System"
  103.       FontSize        =   9.75
  104.       FontStrikethru  =   0   'False
  105.       FontUnderline   =   0   'False
  106.       Height          =   480
  107.       Index           =   2
  108.       Left            =   2040
  109.       TabIndex        =   14
  110.       Top             =   1800
  111.       Width           =   480
  112.    End
  113.    Begin CommandButton Number 
  114.       Caption         =   "3"
  115.       FontBold        =   -1  'True
  116.       FontItalic      =   0   'False
  117.       FontName        =   "System"
  118.       FontSize        =   9.75
  119.       FontStrikethru  =   0   'False
  120.       FontUnderline   =   0   'False
  121.       Height          =   480
  122.       Index           =   3
  123.       Left            =   1320
  124.       TabIndex        =   3
  125.       Top             =   1800
  126.       Width           =   480
  127.    End
  128.    Begin CommandButton Number 
  129.       Caption         =   "2"
  130.       FontBold        =   -1  'True
  131.       FontItalic      =   0   'False
  132.       FontName        =   "System"
  133.       FontSize        =   9.75
  134.       FontStrikethru  =   0   'False
  135.       FontUnderline   =   0   'False
  136.       Height          =   480
  137.       Index           =   2
  138.       Left            =   720
  139.       TabIndex        =   2
  140.       Top             =   1800
  141.       Width           =   480
  142.    End
  143.    Begin CommandButton Number 
  144.       Caption         =   "1"
  145.       FontBold        =   -1  'True
  146.       FontItalic      =   0   'False
  147.       FontName        =   "System"
  148.       FontSize        =   9.75
  149.       FontStrikethru  =   0   'False
  150.       FontUnderline   =   0   'False
  151.       Height          =   480
  152.       Index           =   1
  153.       Left            =   120
  154.       TabIndex        =   1
  155.       Top             =   1800
  156.       Width           =   480
  157.    End
  158.    Begin CommandButton Operator 
  159.       Caption         =   "-"
  160.       FontBold        =   -1  'True
  161.       FontItalic      =   0   'False
  162.       FontName        =   "System"
  163.       FontSize        =   9.75
  164.       FontStrikethru  =   0   'False
  165.       FontUnderline   =   0   'False
  166.       Height          =   480
  167.       Index           =   3
  168.       Left            =   2640
  169.       TabIndex        =   13
  170.       Top             =   1200
  171.       Width           =   480
  172.    End
  173.    Begin CommandButton Operator 
  174.       Caption         =   "+"
  175.       FontBold        =   -1  'True
  176.       FontItalic      =   0   'False
  177.       FontName        =   "System"
  178.       FontSize        =   9.75
  179.       FontStrikethru  =   0   'False
  180.       FontUnderline   =   0   'False
  181.       Height          =   480
  182.       Index           =   1
  183.       Left            =   2040
  184.       TabIndex        =   12
  185.       Top             =   1200
  186.       Width           =   480
  187.    End
  188.    Begin CommandButton Number 
  189.       Caption         =   "6"
  190.       FontBold        =   -1  'True
  191.       FontItalic      =   0   'False
  192.       FontName        =   "System"
  193.       FontSize        =   9.75
  194.       FontStrikethru  =   0   'False
  195.       FontUnderline   =   0   'False
  196.       Height          =   480
  197.       Index           =   6
  198.       Left            =   1320
  199.       TabIndex        =   6
  200.       Top             =   1200
  201.       Width           =   480
  202.    End
  203.    Begin CommandButton Number 
  204.       Caption         =   "5"
  205.       FontBold        =   -1  'True
  206.       FontItalic      =   0   'False
  207.       FontName        =   "System"
  208.       FontSize        =   9.75
  209.       FontStrikethru  =   0   'False
  210.       FontUnderline   =   0   'False
  211.       Height          =   480
  212.       Index           =   5
  213.       Left            =   720
  214.       TabIndex        =   5
  215.       Top             =   1200
  216.       Width           =   480
  217.    End
  218.    Begin CommandButton Number 
  219.       Caption         =   "4"
  220.       FontBold        =   -1  'True
  221.       FontItalic      =   0   'False
  222.       FontName        =   "System"
  223.       FontSize        =   9.75
  224.       FontStrikethru  =   0   'False
  225.       FontUnderline   =   0   'False
  226.       Height          =   480
  227.       Index           =   4
  228.       Left            =   120
  229.       TabIndex        =   4
  230.       Top             =   1200
  231.       Width           =   480
  232.    End
  233.    Begin CommandButton CancelEntry 
  234.       Caption         =   "CE"
  235.       FontBold        =   -1  'True
  236.       FontItalic      =   0   'False
  237.       FontName        =   "System"
  238.       FontSize        =   9.75
  239.       FontStrikethru  =   0   'False
  240.       FontUnderline   =   0   'False
  241.       Height          =   480
  242.       Left            =   2640
  243.       TabIndex        =   11
  244.       Top             =   600
  245.       Width           =   480
  246.    End
  247.    Begin CommandButton Cancel 
  248.       Caption         =   "C"
  249.       FontBold        =   -1  'True
  250.       FontItalic      =   0   'False
  251.       FontName        =   "System"
  252.       FontSize        =   9.75
  253.       FontStrikethru  =   0   'False
  254.       FontUnderline   =   0   'False
  255.       Height          =   480
  256.       Left            =   2040
  257.       TabIndex        =   10
  258.       Top             =   600
  259.       Width           =   480
  260.    End
  261.    Begin CommandButton Number 
  262.       Caption         =   "9"
  263.       FontBold        =   -1  'True
  264.       FontItalic      =   0   'False
  265.       FontName        =   "System"
  266.       FontSize        =   9.75
  267.       FontStrikethru  =   0   'False
  268.       FontUnderline   =   0   'False
  269.       Height          =   480
  270.       Index           =   9
  271.       Left            =   1320
  272.       TabIndex        =   9
  273.       Top             =   600
  274.       Width           =   480
  275.    End
  276.    Begin CommandButton Number 
  277.       Caption         =   "8"
  278.       FontBold        =   -1  'True
  279.       FontItalic      =   0   'False
  280.       FontName        =   "System"
  281.       FontSize        =   9.75
  282.       FontStrikethru  =   0   'False
  283.       FontUnderline   =   0   'False
  284.       Height          =   480
  285.       Index           =   8
  286.       Left            =   720
  287.       TabIndex        =   8
  288.       Top             =   600
  289.       Width           =   480
  290.    End
  291.    Begin CommandButton Number 
  292.       Caption         =   "7"
  293.       FontBold        =   -1  'True
  294.       FontItalic      =   0   'False
  295.       FontName        =   "System"
  296.       FontSize        =   9.75
  297.       FontStrikethru  =   0   'False
  298.       FontUnderline   =   0   'False
  299.       Height          =   480
  300.       Index           =   7
  301.       Left            =   120
  302.       TabIndex        =   7
  303.       Top             =   600
  304.       Width           =   480
  305.    End
  306.    Begin Label Readout 
  307.       Alignment       =   1  'Right Justify
  308.       BackColor       =   &H0000FFFF&
  309.       BorderStyle     =   1  'Fixed Single
  310.       Caption         =   "0."
  311.       FontBold        =   -1  'True
  312.       FontItalic      =   0   'False
  313.       FontName        =   "MS Sans Serif"
  314.       FontSize        =   13.5
  315.       FontStrikethru  =   0   'False
  316.       FontUnderline   =   0   'False
  317.       ForeColor       =   &H00000000&
  318.       Height          =   375
  319.       Left            =   120
  320.       TabIndex        =   19
  321.       Top             =   105
  322.       Width           =   3000
  323.    End
  324. ' ------------------------------------------------------------------------
  325. '               Copyright (C) 1991 Microsoft Corporation
  326. ' You have a royalty-free right to use, modify, reproduce and distribute
  327. ' the Sample Application Files (and/or any modified version) in any way
  328. ' you find useful, provided that you agree that Microsoft has no warranty,
  329. ' obligations or liability for any Sample Application Files.
  330. ' ------------------------------------------------------------------------
  331. ' Modifications & Enhancements by Marcus Smaby - these modifications
  332. ' & enhancements released to the public domain
  333. Dim Op1 As Double           ' Previously input operand.
  334. Dim Op2 As Double           ' Second operand.
  335. Dim DecimalFlag As Integer  ' Decimal point present yet?
  336. Dim NumOps As Integer       ' Number of operands.
  337. Dim LastInput As String     ' Indicate type of last keypress.
  338. Dim OpFlag As String        ' Indicate pending operation.
  339. Dim hWd As Integer          ' Handle to Control
  340. Dim GoodKey As Integer      ' Was this a key we can process
  341. Dim KeyState As String      ' Remember state of Key to prevent repeat
  342. Dim LastKey As Integer      ' scan code of last key
  343. Const True = -1
  344. Const FALSE = 0
  345. ' Click event procedure for C (cancel) key.
  346. ' Reset the display and initializes variables.
  347. Sub Cancel_Click ()
  348.     Readout.Caption = "0."
  349.     Form_Load
  350. End Sub
  351. Sub Cancel_KeyDown (KeyCode As Integer, Shift As Integer)
  352.      LastKey = KeyCode
  353.      ProcessKeyDown
  354. End Sub
  355. Sub Cancel_KeyUp (KeyCode As Integer, Shift As Integer)
  356.     ProcessKeyUp
  357. End Sub
  358. ' Click event procedure for CE (cancel entry) key.
  359. Sub CancelEntry_Click ()
  360.     Readout.Caption = "0."
  361.     DecimalFlag = False
  362.     LastInput = "CE"
  363. End Sub
  364. Sub CancelEntry_KeyDown (KeyCode As Integer, Shift As Integer)
  365.      LastKey = KeyCode
  366.      ProcessKeyDown
  367. End Sub
  368. Sub CancelEntry_KeyUp (KeyCode As Integer, Shift As Integer)
  369.     ProcessKeyUp
  370. End Sub
  371. ' Click event procedure for decimal point (.) key.
  372. ' If last keypress was an operator, initialize
  373. ' readout to "0." Otherwise, append a decimal
  374. ' point to the display.
  375. Sub Decimal_Click ()
  376.     If LastInput <> "NUMS" Then
  377.     Readout.Caption = "0."
  378.     ElseIf DecimalFlag = False Then
  379.     Readout.Caption = Readout.Caption + "."
  380.     End If
  381.     DecimalFlag = True
  382.     LastInput = "NUMS"
  383. End Sub
  384. Sub Decimal_KeyDown (KeyCode As Integer, Shift As Integer)
  385.      LastKey = KeyCode
  386.      ProcessKeyDown
  387. End Sub
  388. Sub Decimal_KeyUp (KeyCode As Integer, Shift As Integer)
  389.     ProcessKeyUp
  390. End Sub
  391. ' Initialization routine for the form.
  392. ' Set all variables to initial values.
  393. Sub Form_Load ()
  394.     DecimalFlag = False
  395.     NumOps = 0
  396.     LastInput = "NONE"
  397.     OpFlag = " "
  398. End Sub
  399. ' Click event procedure for number keys (0-9).
  400. ' Appends new number to the number in the display.
  401. Sub Number_Click (Index As Integer)
  402.     If LastInput <> "NUMS" Then
  403.     Readout.Caption = ""
  404.     DecimalFlag = False
  405.     End If
  406.     Readout.Caption = Readout.Caption + Number(Index).Caption
  407.     LastInput = "NUMS"
  408. End Sub
  409. Sub Number_KeyDown (Index As Integer, KeyCode As Integer, Shift As Integer)
  410.      LastKey = KeyCode
  411.      ProcessKeyDown
  412. End Sub
  413. Sub Number_KeyUp (Index As Integer, KeyCode As Integer, Shift As Integer)
  414.     ProcessKeyUp
  415. End Sub
  416. ' Click event procedure for operator keys (+, -, x, /, =).
  417. ' If the immediately preceeding keypress was part of a
  418. ' number, increment NumOps. If one operand is present,
  419. ' set Op1. If two are present, set Op1 equal to the
  420. ' result of the operation on Op1 and the current
  421. ' input string, and display the result.
  422. Sub Operator_Click (Index As Integer)
  423.     If LastInput = "NUMS" Then
  424.     NumOps = NumOps + 1
  425.     End If
  426.     If NumOps = 1 Then
  427.     Op1 = Val(Readout.Caption)
  428.     ElseIf NumOps = 2 Then
  429.     Op2 = Val(Readout.Caption)
  430.     Select Case OpFlag
  431.         Case "+"
  432.         Op1 = Op1 + Op2
  433.         Case "-"
  434.         Op1 = Op1 - Op2
  435.         Case "X"
  436.         Op1 = Op1 * Op2
  437.         Case "/"
  438.         If Op2 = 0 Then
  439.            MsgBox "Can't divide by zero", 48, "Calculator"
  440.         Else
  441.            Op1 = Op1 / Op2
  442.         End If
  443.         Case "="
  444.         Op1 = Op2
  445.     End Select
  446.     Readout.Caption = Format$(Op1)
  447.     NumOps = 1
  448.     End If
  449.     LastInput = "OPS"
  450.     OpFlag = Operator(Index).Caption
  451. End Sub
  452. Sub Operator_KeyDown (Index As Integer, KeyCode As Integer, Shift As Integer)
  453.      LastKey = KeyCode
  454.      ProcessKeyDown
  455. End Sub
  456. Sub Operator_KeyUp (Index As Integer, KeyCode As Integer, Shift As Integer)
  457.     ProcessKeyUp
  458. End Sub
  459. ' Click event procedure for percent key (%).
  460. ' Compute and display a percentage of the first operand.
  461. Sub Percent_Click ()
  462.     Readout.Caption = Format$(Op1 * Val(Readout.Caption) / 100)
  463. End Sub
  464. Sub Percent_KeyDown (KeyCode As Integer, Shift As Integer)
  465.      LastKey = KeyCode
  466.      ProcessKeyDown
  467. End Sub
  468. Sub Percent_KeyUp (KeyCode As Integer, Shift As Integer)
  469.     ProcessKeyUp
  470. End Sub
  471. ' Key Table
  472. '   + = 43, - = 45, / = 47 * = 42
  473. '   % = 37, = = 61, c = 99,67, . = 46
  474. Sub ProcessKeyDown ()
  475.     If KeyState = "DOWN" Then Exit Sub  ' Avoid Auto Repeat
  476.      ' classify keystroke & act
  477.      ' Debug.Print lastkey
  478.     Select Case LastKey
  479.      Case 48 To 57   ' 0 - 9
  480.         Number(LastKey - 48).SetFocus
  481.         Number_Click (LastKey - 48)
  482.         GoodKey = True
  483.      Case KEY_NUMPAD0 To KEY_NUMPAD9
  484.         Number(LastKey - KEY_NUMPAD0).SetFocus
  485.         Number_Click (LastKey - KEY_NUMPAD0)
  486.         GoodKey = True
  487.      Case 187                    '  =
  488.         Operator(4).SetFocus
  489.         Operator_Click (4)
  490.         GoodKey = True
  491.      Case KEY_SUBTRACT, 45       ' -
  492.         Operator(3).SetFocus
  493.         Operator_Click (3)
  494.         GoodKey = True
  495.      Case KEY_MULTIPLY, 42       ' *
  496.         Operator(2).SetFocus
  497.         Operator_Click (2)
  498.         GoodKey = True
  499.      Case KEY_DIVIDE, 47        ' /
  500.         Operator(0).SetFocus
  501.         Operator_Click (0)
  502.         GoodKey = True
  503.      Case KEY_ADD, 43           ' +
  504.         Operator(1).SetFocus
  505.         Operator_Click (1)
  506.         GoodKey = True
  507.      Case KEY_DECIMAL, 46       ' .
  508.         Decimal.SetFocus
  509.         Decimal_Click
  510.         GoodKey = True
  511.      Case 80, 112               ' p for percent
  512.         Percent.SetFocus
  513.         Percent_Click
  514.         GoodKey = True
  515.      Case 67, 99                 ' C for Cancel
  516.         Cancel.SetFocus
  517.         Cancel_Click
  518.         GoodKey = True
  519.      Case 8, 127                 ' <- or del for CancelEntry
  520.         CancelEntry.SetFocus
  521.         CancelEntry_Click
  522.         GoodKey = True
  523.    End Select
  524.    If GoodKey Then
  525.     hWd = getfocus()    'Get the Handle
  526.     x = sendmessage(hWd, BM_SETSTATE, 1, 0)  ' Set control as pressed
  527.    End If
  528.    KeyState = "DOWN"
  529. End Sub
  530. Sub ProcessKeyUp ()
  531.     x = sendmessage(hWd, BM_SETSTATE, 0, 0) ' set control as unpressed
  532.     KeyState = "UP"
  533.     Operator(4).SetFocus    ' Set Focus to Equals Key
  534. End Sub
  535.