| 
 6.
Menu-Driven and Calculator Projects 
 
Running
the
Project: 
Select
Start
from
the
Run
Menu.   Then
by
clicking
the
menu
items
draw
the
word
hello
for
three
different
thickness.  
The
final
run
mode
window
will
look
as
shown
in
the
figure
4. 
  
           
               Figure
4 
 
2.
Calculator
Project: 
The
basic
layout
of
the
screen
is
shown
in
the
figure
5.   There
are
two
control
arrays
of
buttons
one
for
the
digits
0 to
9
and
another
for
operators
+,
-,
/, *
and
=.   The
form
is
given
the
name
‘calculator’
and
its
icon
property
is
set
to
calculator
icon.   The
display
label
is
given
the
name
‘Readout’
and
the
caption
‘0.’.
The
number
buttons
0 to
9  are
given
the
same
name
‘Number’,
their
captions
and
indexes
are
set
as
0,
1,
2,
3,
4,
5,
6,
7, 8
and
9
respectively.    The
operator
buttons
/,
+,
X, -
and
=
are
given
the
same
name,
“Operator”
,
and
their
indexes
are
set
as
0,
1,
2, 3
and
4
respy.   The
‘%’ 
button
is
given
the
name
‘Percent’,
the
‘.’ 
button
is
given
the
name
‘Decimal’,
the
‘C’
button
is
given
the
name
‘Cancel’
and
the
‘CE’
button
is
given
the
name
‘CancelEntry’.    The
codes
are
entered
in
the
code
window  as
shown
in
the
figure
5a.   Save
and
run
the
project
and  check
that
the
calculator
functions
properly.    
  
  
                            Figure
5 
Option
Explicit 
Dim
Op1,
Op2                '
Previously
input
operand. 
Dim
DecimalFlag
As
Integer  '
Decimal
point
present
yet? 
Dim
NumOps
As
Integer       '
Number
of
operands. 
Dim
LastInput               '
Indicate
type
of
last
keypress
event. 
Dim
OpFlag                  '
Indicate
pending
operation. 
Dim
TempReadout 
'
Click
event
procedure
for
C
(cancel)
key. 
'
Reset
the
display
and
initializes
variables. 
Private
Sub
Cancel_Click() 
    Readout
=
Format(0,
"0.") 
    Op1
= 0 
    Op2
= 0 
    Form_Load 
End
Sub 
'
Click
event
procedure
for
CE
(cancel
entry)
key. 
Private
Sub
CancelEntry_Click() 
    Readout
=
Format(0,
"0.") 
    DecimalFlag
=
False 
    LastInput
=
"CE" 
End
Sub 
'
Click
event
procedure
for
decimal
point
(.)
key. 
'
If
last
keypress
was
an
operator,
initialize 
'
readout
to
"0."
Otherwise,
append
a
decimal 
'
point
to
the
display. 
Private
Sub
Decimal_Click() 
    If
LastInput
=
"NEG"
Then 
        Readout
=
Format(0,
"-0.") 
    ElseIf
LastInput
<>
"NUMS"
Then 
        Readout
=
Format(0,
"0.") 
    End
If 
    DecimalFlag
=
True 
    LastInput
=
"NUMS" 
End
Sub 
  
'
Initialization
routine
for
the
form. 
'
Set
all
variables
to
initial
values. 
Private
Sub
Form_Load() 
    DecimalFlag
=
False 
    NumOps
= 0 
    LastInput
=
"NONE" 
    OpFlag
=
"
" 
    Readout
=
Format(0,
"0.") 
    'Decimal.Caption
=
Format(0,
".") 
End
Sub 
'
Click
event
procedure
for
number
keys
(0-9). 
'
Append
new
number
to
the
number
in
the
display. 
Private
Sub
Number_Click(Index
As
Integer) 
    If
LastInput
<>
"NUMS"
Then 
        Readout
=
Format(0,
".") 
       
DecimalFlag
=
False 
    End
If 
    If
DecimalFlag
Then 
        Readout
=
Readout
+
Number(Index).Caption 
    Else 
        Readout
=
Left(Readout,
InStr(Readout,
Format(0,
"."))
- 1)
+
Number(Index).Caption
+
Format(0,
".") 
    End
If 
    If
LastInput
=
"NEG"
Then
Readout
=
"-"
&
Readout 
    LastInput
=
"NUMS" 
End
Sub 
'
Click
event
procedure
for
operator
keys
(+,
-,
x,
/,
=). 
'
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
Operator_Click(Index
As
Integer) 
    TempReadout
=
Readout 
    If
LastInput
=
"NUMS"
Then 
        NumOps
=
NumOps
+ 1 
   
End
If 
    Select
Case
NumOps 
        Case
0 
        If
Operator(Index).Caption
=
"-"
And
LastInput
<>
"NEG"
Then 
            Readout
=
"-"
&
Readout 
            LastInput
=
"NEG" 
        End
If 
        Case
1 
        Op1
=
Readout 
        If
Operator(Index).Caption
=
"-"
And
LastInput
<>
"NUMS"
And
OpFlag
<>
"="
Then 
            Readout
=
"-" 
            LastInput
=
"NEG" 
        End
If 
        Case
2 
        Op2
=
TempReadout 
        Select
Case
OpFlag 
            Case
"+" 
                Op1
=
CDbl(Op1)
+
CDbl(Op2) 
            Case
"-" 
                Op1
=
CDbl(Op1)
-
CDbl(Op2) 
            Case
"X" 
                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 
        Readout
=
Op1 
        NumOps
= 1 
    End
Select 
    If
LastInput
<>
"NEG"
Then 
        LastInput
=
"OPS" 
        OpFlag
=
Operator(Index).Caption 
    End
If 
End
Sub 
'
Click
event
procedure
for
percent
key
(%). 
'
Compute
and
display
a
percentage
of
the
first
operand. 
Private
Sub
Percent_Click() 
    Readout
=
Readout
/
100 
    LastInput
=
"Ops" 
    OpFlag
=
"%" 
    NumOps
=
NumOps
+ 1 
    DecimalFlag
=
True 
End
Sub 
  
Figure
5a 
 
            |