|
learn
visual basic -- 3, hello world program
in
this chapter, we are going to make a hello world prgram. here,
i'll demonstrate a few methods and keywords in coding VB programs.
codes that you need to type in are written in green and those
already written are in pink, so that you can take note of
them, copy and paste them into your code window. must thank
me for being helpful ok?
creating
a project
first things first, you'll have to create a project. for VB
users, just open the IDE. for VBA users, we can go to Microsoft
Powerpoint (or any other Microsoft office tool), tools>macros>visual
basic editor.
in
the editor or IDE, go to file>new project. a window pops
up and we will now select empty project in VBA or standard
EXE in VB. a form is automatically created for you. you can
open the project explorer now and view the branch files contained
in the project.
working
with controls and form
in the project explorer, open the forms folder. you'll see
a form1 in VB or a userform1 in VBA. edit the form by double
clicking it. the form is now in form mode, in which you can
edit it's properties. in code mode however, you'll edit the
code for the form and it's control. i will talk about code
mode and code window later.
the
form is a grey box with a titlebar and the maximise, minimise
and close button. all arranged as it would be seen when running
as a program independantly. in VB, the toolbox appears on
the left hand side by default, while in VBA, the toolbox pops
up when you click on the form(or open it from the view menu).
let's
look at the toolbox. move your move over the buttons to see
its description. take not of these:
label
textbox
commandbutton
we
are going to use these three controls in our hello world program
later.
place
one of each of these three controls on the form by clicking
on the button, then move the mouse over the form, and then
drag the area of the control on the form. try it. a single
click on the controls in the form will change the contents
of the properties window. click on the commandbutton you just
created in the form. go to the properties window, scroll and
look for the property "Caption" in the left column.
change the corresponding left column to "Greet".
notice that there are two views for the properties, the alphabet
and the catergory. both shows the same properties however.
change
the properties of the following:
commandbutton
name - cmd1
caption - Greet
label
name - lbl1
caption - This is my first hello world
program!
textbox
name - txt1
text - Enter greet message here
we
can rewrite the above three instruction as codes as follow:
cmd1.caption
= "Greet"
lbl1.caption = "This is my first hello world program!"
txt1.text = "Enter greet message here"
if
you have figured out why, then you are very smart. if not,
you are not stupid either, and i will tell you what it is
later.
working
with codes
we are about the enter codes into the program. to bring up
the code window, just double click on the commandbutton, which
is named "cmd1".
the
code window opens. at the top of the code window, you'll see
two drop down boxes (textboxes with down arrow at the right).
on the left you'll see "cmd1", which refers to the
commandbutton you created. on the right, you'll see action
"click". below them, you see the codes:
Private
Sub cmd1_Click()
End
Sub
right
in between the two lines, type in the following so that it
looks like:
Private
Sub cmd1_Click()
MsgBox "Hello friend", , "Hello
World"
End Sub
click
on the run button in the toolbar. the program is now being
tested. click on the command button. and a message box appear
with the prompt "Hello friend" and the title "Hello
World". go back to the code view by closing the running
form either by means of the stop button on the toolbar or
the close button on the form. a close look at the code enables
you to decipher the meaning.
MsgBox
indeed is a keyword for bring up a message box. "Hello
friend" and "Hello world" are parameters, a
non-fixed or variable content, for use with the MsgBox method.
one being the prompt and the other the title. note that they
are in double quots ("...") also. try changing the
two for different results.
all
procedures are enclosed by two lines -- "Private Sub
..." and "End Sub". therefore, a Sub is a procedure
in VB. "cmd1_click()" indicates the condition for
the procedure to be executed. in this case, "cmd1"
refers to the named commandbutton cmd1 and the event of clicking
it will execute the procedure. if the commandbutton is named
somthing esle like "iloveyou" then it will become
"iloveyou_click()". there are other events for a
command button if you look at the right drop down box, which
is also the event drop down box, and when the left drop down
box, or the object drop down box, is set at "cmd1".
so
what is the use of the txt1 and lbl1? we will use it now.
go back to code view, and enter the code:
Private
Sub cmd1_Click()
greeting = txt1.Text
MsgBox greeting, , "Hello World"
End Sub
run
the program. type a greeting message in the textbox, such
as: good day sir. then click on cmd1. a MsgBox pops up and
show the prompt "good day sir" and the title "Hello
World". what happened? let's stop the program and go
back to the code.
"greeting"
is a variable that we defined. it is not in quots, and can
be named anything. replace the two appearances of the word
word greeting to greet and run the program, you'll get the
same results. try it.
a
variable can take any value if not resrticted (you can restrict
it to a type of value if you want). here, the variable greeting
is assigned, with the assignment operator "=" (equals),
with the value txt1.text. you can read the whole line as follow
and you'll see it is english:
greeting
equals to txt1.text.
so
what is txt1.text? scroll all the way up to where we first
changed properties of objects and read the part again. txt1
is the name of the textbox. and "text" is the property
of txt1. therefore, txt1.text means:
value
of text of txt1
the
dot is called the period, which separates the property from
the object.
so
here, greeting takes any value, be it a number or a sentence,
and becomes that value. and the next line:
MsgBox
greeting, , "Hello World"
greeting
appears again. from the fist line of code, greeting now has
the value of the text of txt1. with this value, greeting acts
as the first parameter of MsgBox, which is the prompt for
the message box. therefore, the message box brought up show
the message you typed in txt1. note once again that greeting
is not enclosed by quots.
what
the fuss about quots? why keep talking about it. well..it
is one very common mistake programmers make. forgetting about
the quots. a line of text, a word or sentence with qouts is
called a string. a string is a sentence which will appear
as it is. just like "Hello friend" appears as Hello
Friend but without quots, it can be a line of code or a variable,
which does not appear as it is. greeting does not appear as
"greeting", it appears as whatever is the value
of txt1.text.
for
numbers, a number is a number. it cannot be a variable own
it's own without alphabets (ie. greeting1 is a variable but
1 is not). you can force it to act like a string if you enclose
it in quots.
i explain further in the next chapter.
for
now, you can learn how to save a project. just click the save
button on the toolbar. you'll be asked to save twice, once
as a .vbp or visual basic project and the other one as .frm
or form. usaually, for simplicity's sake, save both with a
common name, thus helloworld.vbp and helloworld.frm.
the
.vpb files contains linkage and code information and the .frm
files contains form layout information. when you open the
files next time, only open the .vbp files and all the other
related files will be loaded.
if
using VBA, the program is saved together with the .ppt (or
the microsoft office you are using VBA from). so opening the
.ppt will mean opening the VBA.
continue
next chapter
|