home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / ch_code / ch02 / math / mathcalc.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-05-31  |  7.7 KB  |  271 lines

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