|
Example 4.1
Private Sub
Command1_click
Label1.Visible=false
Label2.Visible=True
Text1.Text=”You are
correct!”
End sub
Example 4.2
Private Sub
Command1_click
Label1.Caption=”
Welcome”
Image1.visible=true
End sub
Example 4.3
Private Sub
Command1_click
Pictuire1.Show=true
Timer1.Enabled=True
Lable1.Caption=”Start
Counting
End sub
In
example 4.1, clicking on the command button will make
label1 become invisible and label2 become visible; and
the text” You are correct” will appear in TextBox1.
In example 4.2, clicking on the command button will
make the caption label1 change to “Welcome” and Image1
will become visible. In example 4.3 , clicking on
the command button will make Picture1 show up, timer
starts running and the caption of label1 change to
“Start Counting”.
Syntaxes that do not
involve setting of properties are also English-like,
some of the commands are Print, If…Then….Else….End
If, For…Next, Select Case…..End Select , End and
Exit Sub. For example, Print “ Visual Basic”
is to display the text Visual Basic on screen and
End is to end the program. Other commands will be
explained in details in the coming lessons.
Program code that involve calculations is very easy to
write, you need to write them almost like you do in
mathematics. However, in order to write an event
procedure that involves calculations, you need to know
the basic arithmetic operators in VB as they are not
exactly the same as the normal operators we use,
except for + and - . For multiplication,
we use *, for division we use /, for
raising a number x to the power of n, we use x ^n
and for square root, we use Sqr(x). VB
offers many more advanced mathematical functions such
as Sin,
Cos, Tan
and Log, they will be discussed in lesson 10.
There are also two important functions that are
related to arithmetic operations, i.e. the functions
Val and Str$ where Val is to convert
text entered into a textbox to numerical value and Str$
is to display a numerical value in a textbox as a
string (text). While the function Str$ is as
important as VB can display a numeric values as string
implicitly, failure to use Val will results in wrong
calculation. Let’s examine example 4.4 and example
4.5.
Example 4.4
Private Sub
Form_Activate()
Text3.text=text1.text+text2.text
End Sub
|
Example
4.5
Private Sub
Form_Activate()
Text3.text=val(text1.text)+val(text2.text)
End Sub
|
When you run the program in
example 4.4 and enter 12 in textbox1 and 3 in textbox2
will give you a result of 123, which is wrong. It is
because VB treat the numbers as string and so it just
joins up the two strings. On the other hand, running
exampled 4.5 will give you the correct result, i.e.,
15.
|