Visual Basic is an 'event-driven' language - this means that certain events
such as clicking a button, choosing a menu item or pressing a key on the
keyboard can trigger a sequence of actions in the program (a 'subroutine' or
'procedure'). Let's see how the first type of event, the clicking of a button, could be coded.Imagine that the form contains a button called
Button1 and that we want to give a "Hello" greeting when the button is
clicked by the user. Double-clicking the button opens up the code window and we
see that several lines of code have been automatically generated for us already:
Public Class Form1
Private Sub Button1_Click(BLAH BLAH) Handles
Button1.Click
End Sub
End Class
The first and last lines are always needed but I won't try to
explain them here nor will I bother showing them in all my code
examples. Just remember: those lines are always needed.
Visual Basic also puts a lot of hard-to-understand stuff into the
line beginning 'Private Sub' but fortunately you don't need to
understand this either at this time so I have just replaced it with
'BLAH BLAH' above.
These two lines 'Private Sub' and 'End Sub' are called 'wrappers' - they mark the beginning and end of
the subroutine and therefore the code we write must be inserted between these
wrappers. Note the word 'sub' in the first line - this is short for subroutine,
of course. At the end of the first line we see that this subroutine will be
activated when Button1 is clicked. The parentheses are empty here but sometimes
they will contain variables called 'parameters' that are used to pass
information to the subroutine.
Add a line of code and the subroutine will
be complete:
Private Sub Button1_Click(BLAH BLAH) Handles
Button1.Click
MsgBox ("Hello")
End Sub
MsgBox is short for 'messagebox' and will display your message on the screen.
Your program is now complete! Click the Run icon
(or press the F5 key) and test your program by clicking the button. Stop
the program by hitting the stop icon
or by clicking the X in the top-right corner of the form (don't close VB
itself!).
Now let's extend the code to add an 'input' instruction - we will
make the computer ask the user for his or her name. Modify the code so it looks
like this:
Private Sub Button1_Click(BLAH BLAH) Handles
Button1.Click
UserName = InputBox("Enter your name")
MsgBox ("Hello, " & UserName)
End Sub
The second line displays an 'inputbox' that asks the user to enter his or her
name, and then stores the answer in a variable called UserName. A variable is
like a container - it can contain numbers or text, and the contents of the
variable can change while the program is running - hence the name variable!
You've met variables before in your math classes, hopefully. Note that variable
names can't contain spaces - this variable could not be called 'User Name'.
Interestingly, the name 'Name' is also not allowed, for it is one a few dozen
'keywords' that are reserved by Visual Basic.
Look again at the second line.
Note how the equals sign is being used in a way here that is very different to
its use in your math class. In VB, the equals sign is usually used to modify the
contents of a variable, or to change the properties of a control. When used in
this way, the equals sign is called the 'assignment operator'. Notice that the variable
on the LEFT of the operator is assigned the value given on the RIGHT of the
operator.
Imagine that your program uses a variable called x. What would the
following line do?
x = x + 1
This line (which would make no sense in a math class!) works out the value of
the expression on the right by adding one to the existing value of x and then
stores the result back into the variable called x. In other words, this line has the effect of
adding one to x.
Sometimes the equals sign is used as in your math class, as
in the following line:
If (x = y) then
MsgBox ("X and y are the same.")
This line checks to see whether the variables x and y have the same value and
if they do then the computer displays a message.
So by now you should have
understood that the line
UserName = InputBox("Enter your name")
will display an
inputbox into which you can type your name and then will take your name
and store it in the variable called UserName. The following line then
displays a messagebox containing the word "Hello, " followed by the name
that was stored in the UserName container (variable). the ampersand
character '&' is used to join the name to the word "hello" to form a
'text string' (a string of characters) that the messagebox can display.
There is one further complication: Visual Basic normally does not allow
us to use variables that have not previously been 'declared'. However,
by including a special statement 'Option Explicit Off' at the
beginning of our code we can do away with the need to declare variables.
Declaring variables is a good idea, however, since it protects your
program from errors, so you will learn how to do that later.
Thus the
full code (but still with BLAH BLAH) that we have developed on this page
looks like this:
Option Explicit Off
Public Class Form1
Private Sub Button1_Click(BLAH BLAH) Handles
Button1.Click
UserName = InputBox("Enter your name")
MsgBox ("Hello, " & UserName)
End Sub
End Class
You will notice that
there are some minus signs in small boxes at the left side of the Code
Window - clicking these allows you to temporarily hide procedures so
that they do not distract you while you are trying to work on other
procedures. You can unhide hidden procedures by clicking on the plus
signs.
VB is very fussy about grammar (called 'syntax' in computer programming).
If the syntax is not correct then your program may not run at all! VB tries to
help you avoid syntax errors - it will report errors in the Error List window if
the program still has errors when you try to run ('build') the program
by choosing Debug>Start Debugging or pressing F5.
All computer programs consist of a combination of linear (step-by-step)
sequences of actions, or statements, conditional branches where a test condition
determines what the program will do next, and loops, where the same sequence of
instructions can be repeated many times.