|
learn
visual basic -- 4.2, variables and operators
in
this part of our "journey" we will create simple
calculator. it is really going to be easy, but i think i have
problem telling you how to create it without pictures...now
i know why you cannot find VB tutorials abundant on the web...nevermind.
i have it here! but you'll have to follow closely with me.
create
a new project and create the following controls with the respective
properties:
form1
(or userform1 in VBA)
name -- frm1
caption -- calculator
text1
(or textbox1 in VBA)
name -- txt1
text -- (no text)
text2
name -- txt2
text -- (no text)
commandbutton1
name -- cmd1
caption -- calculate
commandbutton2
name -- cmd2
caption -- exit
label1
name -- lbl1
caption -- Enter one number in each textbox and select the
operator for calculation.
we'll
create a frame and 4 option buttons in step by step. first,
from the toolbox, look for the frame control. click to select
then drag the frame control onto the frm1. then from the toolbox,
select the option button and drag it onto the frame. set their
properties.
frame1
name -- optframe1
option1
name -- opt1
caption -- +
option2
name -- opt2
caption -- -
option3
name -- opt3
caption -- *
option4
name -- opt4
caption -- /
also,
create another label to show the operator between the two
values. ie. place, in a row, txt1 first, then the label then
txt2. set the property of the new label.
label2
name -- lbl2
caption -- (no caption)
in
this program, the user is required to enter two values, one
in txt1 and one in txt2. then the user will select the operator
from the optframe1 with opt1 to opt4. ENSURE that opt1 to
opt4 are in the same, one, optframe1 so that only one option
is selectable at a time. this is to prevent using two operators
at a time to compute the result. when the user selects the
operator, lbl2 will show the operator that is chosen.
to
enter the code, you can double click cmd1 and enter the following:
Private
Sub cmd1_Click()
varno1 = Val(txt1.Text) 'convert txt1.text
to a number
varno2 = Val(txt2.Text) 'convert txt2.text to a number
If opt1.Value = True Then 'the start of if...then statement
block
answer = varno1 + varno2
ElseIf opt2.Value Then
answer = varno1 - varno2
ElseIf lbl2.Caption = "*" Then
answer = varno1 * varno2
ElseIf opt4.Value <> 0 Then
answer = varno1 / varno2
Else
MsgBox "You haven't select an operator", , "Result"
End If 'end of if...then staement block
MsgBox "The answer is " & answer, , "Result"
End Sub
double
click on opt1 and enter the following:
Private
Sub opt1_Click()
lbl2.Caption = "+"
End Sub
double
click on opt2 and enter the following:
Private
Sub opt2_Click()
lbl2.Caption = "-"
End Sub
double
click on opt3 and enter the following:
Private
Sub opt3_Click()
lbl2.Caption = "*"
End Sub
double
click on opt4 and enter the following:
Private
Sub opt4_Click()
lbl2.Caption = "/"
End Sub
you
should do proper indenting in your code like this.
indenting allows you to read the code easier. especially when
you encounter the block statements.
now,
time for explaination. the whole program has code divided
into five event procedures. each procedure here, happens to
be of click() events. when the respective objects are clicked,
the procedures are carried out.
we now take a look at:
Private
Sub opt1_Click()
lbl2.Caption = "+"
End Sub
this
is an event procedure of click of opt1. the actions of this
procedure is:
lbl2.Caption
= "+"
the
caption of lbl2 is assigned the value of "+", a
string value, when opt1 is clicked. by now, this should be
legible. it is the same as assigning a value to a variable.
in fact, the caption of lbl2 is a variable as it can contain
any value. now look at:
varno1
= Val(txt1.Text)
varno1
and varno2 are variables which are assigned the value of Val(txt1.Text).
Val(n) converts the value in txt1.Text to a real number, where
n = txt1.Text. this ensures that we are dealing with numbers
in the calculations and not strings. (so that 1 + 1 = 2 and
not "11"). try removing the Val(n) and see what
happens.
the
two values are then operated under the if...then statement.
what is an if...then statement? we have:
If
opt1.Value = True Then
answer = varno1 + varno2
ElseIf opt2.Value Then
answer = varno1 -varno2
ElseIf lbl2.Caption = "*" Then
answer = varno1 *varno2
ElseIf opt4.Value <> 0 Then
answer = varno1 / varno2
Else
MsgBox "You haven't select an operator", , "Result"
End If
the
first line of condition says:
if opt1.Value equals to true, then answer is assigned the
value of varno1 plus varno2.
opt1.Value
is the value property of opt1, if you check the proerties
window of opt1, you can see that there are only two values,
true or false, or we say that opt1.Value is a boolean value.
to
execute the action of answer = varno1 + varno2 , the condition
specified must be true. therefore, if the value of opt1.Value
is indeed True, then the plus action is carried out.
what
if the first condition is not true? the program continues
on the second line of the if...then block. the next line of
condition reads:
else if opt2.Value then answer is assgned the value of varno1
minus varno2.
if
the condition this time is true, answer is assigned the computed
value.
the
if...then statement block ends with the End If statement.
everything between the If..then and the End If becomes the
statement block. if any one of the condition is true, the
corresponding action is carried out and then the program leaves
the statement block via the End If.
However,
if the all the tested conditions are false:
Else
MsgBox "You haven't select an operator", , "Result"
the
concluding action would be showing a message box telling why
all conditions is false(in this case). you can actually leave
out the else statement, which is carried out if all above
conditions are false, and the program will not execute any
actions in the if...then statement block.
the
if...then statement block demostrates different methods of
comparing values:
the
first specify that opt1.Value must be equal to True, all other
values cannot be excepted for the following action to be executed.
the
second makes use of the boolean value of the opt2.Value. the
value opt2.Value can have is either true or false. therefore,
a true value will executed the actions that follows, before
the next Else If, whereas a false value will tell the program
to try the next condition.
the
third condition shows that strings can be used in comparison
also. if the strings are identical, case sensitive, then the
condtion returned is true. if the strings are not identical,
the the condition returns a false value. only when the condition
returns true, the actions that follow will be carried out.
finally,
the condition states that opt4.Value cannot be or not equal
to (<>) 0. it can be any Value (although there is only
two) but it cannot be 0 for the condition to be true, and
the actions to be executed. recall that 0 is false and 1 or
more is true? therefore, you can use numbers in boolean conditions.
eventually,
the program reaches the end of the if...then statement and
reads on at the MsgBox function, using the & sign, joins
the string and the variable answer as an output for the result.
you
must have also noticed that there is a ' at some lines of
code. the ' (single quot) sign is a commentor. anything that
is on the same line and after the ' , is commented. that means
it is not part of the code, but a comment. you should practice
commenting codes in large programs so that you know what is
what when you edit. also, you can use it to temporarily disable
parts of the code so that it is not executed.
the
Val(n) where n is any value, converts n to a real number.
there are others as follows:
Int(n)
converts n to an integer or rounds down number.
Abs(n)
returns the absolute value of n.
Sqr(n)
square root of n
Rnd(n)
generates a random number form 0 to 1 ie. 0.23 also.
Sin(n)
returns the sine of n in radians
Cos(n)
returns the cosine of n in radians
Tan(n)
returns the tangent of n in radians
more
operators:
+
plus
-
minus
*
multiply
/
divide
<
less than
>
greater than
=
equals to
<=
less than or equal to
>=
more than or equal to
<>
not equal to
this
chapter would have already explained variables and operators
in detail.
next
chapter awaits...
|