home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 5 / MasteringVisualBasic5.iso / ch_code / ch02 / mathcalc.frm (.txt) < prev   
Encoding:
Visual Basic Form  |  1997-02-20  |  9.6 KB  |  323 lines

  1. VERSION 5.00
  2. Begin VB.Form MathCalc 
  3.    Caption         =   "MathCalc"
  4.    ClientHeight    =   3255
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   3510
  8.    LinkTopic       =   "Form2"
  9.    ScaleHeight     =   3255
  10.    ScaleWidth      =   3510
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.CommandButton Over 
  13.       Caption         =   "1/X"
  14.       Height          =   450
  15.       Left            =   2715
  16.       TabIndex        =   19
  17.       Top             =   825
  18.       Width           =   465
  19.    End
  20.    Begin VB.CommandButton PlusMinus 
  21.       Caption         =   "+/-"
  22.       BeginProperty Font 
  23.          Name            =   "Courier New"
  24.          Size            =   9
  25.          Charset         =   0
  26.          Weight          =   400
  27.          Underline       =   0   'False
  28.          Italic          =   0   'False
  29.          Strikethrough   =   0   'False
  30.       EndProperty
  31.       Height          =   450
  32.       Left            =   2115
  33.       TabIndex        =   18
  34.       Top             =   825
  35.       Width           =   465
  36.    End
  37.    Begin VB.CommandButton Digits 
  38.       Caption         =   "1"
  39.       Height          =   450
  40.       Index           =   1
  41.       Left            =   330
  42.       TabIndex        =   17
  43.       Top             =   825
  44.       Width           =   465
  45.    End
  46.    Begin VB.CommandButton Digits 
  47.       Caption         =   "2"
  48.       Height          =   450
  49.       Index           =   2
  50.       Left            =   885
  51.       TabIndex        =   16
  52.       Top             =   825
  53.       Width           =   465
  54.    End
  55.    Begin VB.CommandButton Digits 
  56.       Caption         =   "3"
  57.       Height          =   450
  58.       Index           =   3
  59.       Left            =   1455
  60.       TabIndex        =   15
  61.       Top             =   825
  62.       Width           =   465
  63.    End
  64.    Begin VB.CommandButton Equals 
  65.       Caption         =   "="
  66.       BeginProperty Font 
  67.          Name            =   "Courier New"
  68.          Size            =   12
  69.          Charset         =   0
  70.          Weight          =   400
  71.          Underline       =   0   'False
  72.          Italic          =   0   'False
  73.          Strikethrough   =   0   'False
  74.       EndProperty
  75.       Height          =   450
  76.       Left            =   2115
  77.       TabIndex        =   14
  78.       Top             =   2460
  79.       Width           =   1095
  80.    End
  81.    Begin VB.CommandButton Div 
  82.       Caption         =   "/"
  83.       BeginProperty Font 
  84.          Name            =   "Courier New"
  85.          Size            =   12
  86.          Charset         =   0
  87.          Weight          =   400
  88.          Underline       =   0   'False
  89.          Italic          =   0   'False
  90.          Strikethrough   =   0   'False
  91.       EndProperty
  92.       Height          =   450
  93.       Left            =   2715
  94.       TabIndex        =   13
  95.       Top             =   1915
  96.       Width           =   465
  97.    End
  98.    Begin VB.CommandButton Times 
  99.       Caption         =   "*"
  100.       BeginProperty Font 
  101.          Name            =   "Courier New"
  102.          Size            =   12
  103.          Charset         =   0
  104.          Weight          =   400
  105.          Underline       =   0   'False
  106.          Italic          =   0   'False
  107.          Strikethrough   =   0   'False
  108.       EndProperty
  109.       Height          =   450
  110.       Left            =   2115
  111.       TabIndex        =   12
  112.       Top             =   1915
  113.       Width           =   465
  114.    End
  115.    Begin VB.CommandButton Minus 
  116.       Caption         =   "-"
  117.       BeginProperty Font 
  118.          Name            =   "Courier New"
  119.          Size            =   12
  120.          Charset         =   0
  121.          Weight          =   400
  122.          Underline       =   0   'False
  123.          Italic          =   0   'False
  124.          Strikethrough   =   0   'False
  125.       EndProperty
  126.       Height          =   450
  127.       Left            =   2715
  128.       TabIndex        =   11
  129.       Top             =   1370
  130.       Width           =   465
  131.    End
  132.    Begin VB.CommandButton Plus 
  133.       Caption         =   "+"
  134.       BeginProperty Font 
  135.          Name            =   "Courier New"
  136.          Size            =   12
  137.          Charset         =   0
  138.          Weight          =   400
  139.          Underline       =   0   'False
  140.          Italic          =   0   'False
  141.          Strikethrough   =   0   'False
  142.       EndProperty
  143.       Height          =   450
  144.       Left            =   2115
  145.       TabIndex        =   10
  146.       Top             =   1370
  147.       Width           =   465
  148.    End
  149.    Begin VB.CommandButton ClearBttn 
  150.       Caption         =   "C"
  151.       Height          =   450
  152.       Left            =   330
  153.       TabIndex        =   9
  154.       Top             =   2460
  155.       Width           =   465
  156.    End
  157.    Begin VB.CommandButton DotBttn 
  158.       Caption         =   "."
  159.       BeginProperty Font 
  160.          Name            =   "MS Sans Serif"
  161.          Size            =   12
  162.          Charset         =   0
  163.          Weight          =   700
  164.          Underline       =   0   'False
  165.          Italic          =   0   'False
  166.          Strikethrough   =   0   'False
  167.       EndProperty
  168.       Height          =   450
  169.       Left            =   1455
  170.       TabIndex        =   8
  171.       Top             =   2460
  172.       Width           =   465
  173.    End
  174.    Begin VB.CommandButton Digits 
  175.       Caption         =   "9"
  176.       Height          =   450
  177.       Index           =   9
  178.       Left            =   1455
  179.       TabIndex        =   7
  180.       Top             =   1915
  181.       Width           =   465
  182.    End
  183.    Begin VB.CommandButton Digits 
  184.       Caption         =   "8"
  185.       Height          =   450
  186.       Index           =   8
  187.       Left            =   885
  188.       TabIndex        =   6
  189.       Top             =   1915
  190.       Width           =   465
  191.    End
  192.    Begin VB.CommandButton Digits 
  193.       Caption         =   "7"
  194.       Height          =   450
  195.       Index           =   7
  196.       Left            =   330
  197.       TabIndex        =   5
  198.       Top             =   1915
  199.       Width           =   465
  200.    End
  201.    Begin VB.CommandButton Digits 
  202.       Caption         =   "6"
  203.       Height          =   450
  204.       Index           =   6
  205.       Left            =   1455
  206.       TabIndex        =   4
  207.       Top             =   1370
  208.       Width           =   465
  209.    End
  210.    Begin VB.CommandButton Digits 
  211.       Caption         =   "5"
  212.       Height          =   450
  213.       Index           =   5
  214.       Left            =   885
  215.       TabIndex        =   3
  216.       Top             =   1370
  217.       Width           =   465
  218.    End
  219.    Begin VB.CommandButton Digits 
  220.       Caption         =   "4"
  221.       Height          =   450
  222.       Index           =   4
  223.       Left            =   330
  224.       TabIndex        =   2
  225.       Top             =   1370
  226.       Width           =   465
  227.    End
  228.    Begin VB.CommandButton Digits 
  229.       Caption         =   "0"
  230.       Height          =   450
  231.       Index           =   0
  232.       Left            =   885
  233.       TabIndex        =   1
  234.       Top             =   2460
  235.       Width           =   465
  236.    End
  237.    Begin VB.Label Display 
  238.       Alignment       =   1  'Right Justify
  239.       BorderStyle     =   1  'Fixed Single
  240.       BeginProperty Font 
  241.          Name            =   "Times New Roman"
  242.          Size            =   9.75
  243.          Charset         =   0
  244.          Weight          =   700
  245.          Underline       =   0   'False
  246.          Italic          =   0   'False
  247.          Strikethrough   =   0   'False
  248.       EndProperty
  249.       Height          =   390
  250.       Left            =   330
  251.       TabIndex        =   0
  252.       Top             =   240
  253.       Width           =   2820
  254.    End
  255. Attribute VB_Name = "MathCalc"
  256. Attribute VB_GlobalNameSpace = False
  257. Attribute VB_Creatable = False
  258. Attribute VB_PredeclaredId = True
  259. Attribute VB_Exposed = False
  260. Option Explicit
  261. Dim Operand1 As Double, Operand2 As Double
  262. Dim Operator As String
  263. Dim ClearDisplay As Boolean
  264. Private Sub ClearBttn_Click()
  265.     Display.Caption = ""
  266. End Sub
  267. Private Sub Digits_Click(Index As Integer)
  268.     If ClearDisplay Then
  269.         Display.Caption = ""
  270.         ClearDisplay = False
  271.     End If
  272.     Display.Caption = Display.Caption + Digits(Index).Caption
  273. End Sub
  274. Private Sub Div_Click()
  275.     Operand1 = Val(Display.Caption)
  276.     Operator = "/"
  277.     Display.Caption = ""
  278. End Sub
  279. Private Sub DotBttn_Click()
  280.     If InStr(Display.Caption, ".") Then
  281.         Exit Sub
  282.     Else
  283.         Display.Caption = Display.Caption + "."
  284.     End If
  285. End Sub
  286. Private Sub Equals_Click()
  287. Dim result As Double
  288. On Error GoTo ErrorHandler
  289.     Operand2 = Val(Display.Caption)
  290.     If Operator = "+" Then result = Operand1 + Operand2
  291.     If Operator = "-" Then result = Operand1 - Operand2
  292.     If Operator = "*" Then result = Operand1 * Operand2
  293.     If Operator = "/" And Operand2 <> "0" Then result = Operand1 / Operand2
  294.     Display.Caption = result
  295.     ClearDisplay = True
  296.     Exit Sub
  297. ErrorHandler:
  298.     MsgBox "The operation resulted in the following error" & vbCrLf & Err.Description
  299.     Display.Caption = "ERROR"
  300.     ClearDisplay = True
  301. End Sub
  302. Private Sub Minus_Click()
  303.     Operand1 = Val(Display.Caption)
  304.     Operator = "-"
  305.     Display.Caption = ""
  306. End Sub
  307. Private Sub Over_Click()
  308.     If Val(Display.Caption) <> 0 Then Display.Caption = 1 / Val(Display.Caption)
  309. End Sub
  310. Private Sub Plus_Click()
  311.     Operand1 = Val(Display.Caption)
  312.     Operator = "+"
  313.     Display.Caption = ""
  314. End Sub
  315. Private Sub PlusMinus_Click()
  316.     Display.Caption = -Val(Display.Caption)
  317. End Sub
  318. Private Sub Times_Click()
  319.     Operand1 = Val(Display.Caption)
  320.     Operator = "*"
  321.     Display.Caption = ""
  322. End Sub
  323.