Visual Basic Notes 1
Visual Basic Notes 1
MODULE TWO
VISUAL BASIC is a high level programming language evolved from the earlier DOS
version called BASIC. BASIC stands for Beginners' All-purpose Symbolic Instruction
Code. The program codes in Visual Basic resemble the English language. Different
software companies produce different versions of BASIC, such as Microsoft QBASIC,
QUICKBASIC, GWBASIC, and IBM BASICA and so on.
Visual Basic is a fairly easy programming language to learn and it is for anybody who is
interested in programming but lack professional training in software engineering.
Learning VB will help one to improve their logical thinking skills and develop
their minds. You can choose to program in VB purely for fun and enjoyment or you can
create more advanced applications such as educational courseware and commercial
software.
VISUAL BASIC is a VISUAL and events driven Programming Language. These are the
main divergences from the old BASIC. In BASIC, programming is done in a text-based
environment and the program is executed sequentially. In VISUAL BASIC,
programming is done in a graphical environment (Graphical User Interface – GUI). In old
BASIC, you have to write a text-based procedure to design the interface, but Visual Basic
enables you to design the interface by dragging and resizing the objects as well as changing
their colors, just like any windows-based programs.
Visual Basic is event-driven because users may click on a certain object randomly, so
each object has to be programmed independently to be able to response to those
actions (events).
Examples of events are clicking a command button, entering text into a text box, selecting
an item in a list box etc. Therefore, a VISUAL BASIC Program is
made up of many subprograms; each with its own program code which can be
executed independently and at the same time can be linked together in one way or
another.
1.2 The Visual Basic Development Environment
Upon start up, Visual Basic 6.0 will display the following dialog box
1.1. You can choose to start a new project, open an existing project or select a list of
recently opened programs. A project is a collection of files that make up your
application. There are various types of applications that can be created; however, we
shall concentrate on creating Standard EXE programs (EXE means executable
program). Now, click on the Standard EXE icon to go into the VB programming
environment.
The Visual Basic Development Environment
Exercise 1
Objectives
In this section, we are not going into the technical aspects of VB programming;
the
purpose of this section is to let you get a feel for it. Now, you can try out a few
examples in this lesson.
Example 2.1 is a simple program. First of all, you have to launch Microsoft Visual
Basic. Normally, a default form Form1 will be available for you
to start your new project. Double click on Form1, and the source code window
for it as shown in Figure 2.1 will appear.
Here, you can see the form, the command button with the name Command1,
the Label with the name Label1 and the PictureBox with the name Picture1.
Similarly, when you click on the procedure box, a list of procedures associated
with the object will be displayed as shown in Figure 2.3.
Some of the procedures associated with the object Form are Activate, Click,
DblClick (Double-Click) , DragDrop, keyPress etc. Each object has its own set of
procedures.
You can select an object and write codes for any of its procedures in order to
perform certain tasks as shown below :-
Form1.show
Print “Welcome to Visual Basic tutorial”
End Sub
You do not have to worry about the beginning and the end statements (i.e.
Private Sub Form_Load.......End Sub.); Just key in the codes between the above
two statements exactly as shown here. When you run the program, don’t be
surprised that nothing shows up.
In order to display the output of the program, you have to add the
Form1.show statement (as shown above) or you can just use the Form_Activate
( ) event procedure as shown below
In the above example, the command Print does not mean printing using a
printer. Instead, it means displaying the output on the computer screen.
Now, press F5 or click on the run button to run the program and you will get the
output as shown below
You can also perform simple arithmetic calculations as shown in example above.
VB uses (*) to denote the multiplication operator and (/)to denote the division
operator.
The output of the above coding will reslutls in output arranged vertically as
shown below .
End Sub
Example
x = 20
y = 10
Print x + y
Print x - y
Print x * y
Print x / y
End Sub
The coding employs two variables x and y and assigns initial values of 20 and 10
to them respectively. When you need to change the values of x and y, just
change the initial values rather than changing every individual value which is
more time consuming.
Besides, you can also use the + or the & operator to join two or more texts
(string)
together like in examples shown below
Example 1
A= "Tom "
“likes "
“to "
“eat "
“burgers."
Print A + B + C + D + E
End Sub
Example 2
A = "Tom "
B = “likes "
C = “to "
D = “eat "
E = “burgers."
Print A & B & C & D & E
End Sub
Generally, there are three basic steps in building a VB application. The steps are
as
follows:
Example
First of all, go to the properties window and change the form caption to
Volume of
Cylinder
Drag and insert three labels into the form and change their captions to
Base Radius, Height and Volume respectively.
Insert three Text Boxes and clear its text contents(get to text property of
the textbox control and delete current content) so that you get three
empty boxes.
Name the text boxes as radius, hght (we cannot use height as it is one of
the built-in control names) and volume respectively.
Insert a command button and change its caption to O.K and its name to
OK.
A Calculator
• Resize the form until you get the size you are satisfied with.
• Go to the properties window and change the default caption of the form to the
caption you like, such as 32 Calculator.
• Change the other properties of the form, such as background color, foreground
color, and border style.
For this particular program, I recommend you set the following properties for
Form1:
These properties will ensure that the users cannot resize or maximize your
calculator window, but able to minimize the window.
• Draw the Display Panel by clicking on the Label button and place your
mouse on the form. Start drawing by pressing down your mouse button and
dragging it.
• Click on the panel and the corresponding properties window will appear. Clear
the default label so that the caption is blank. It is better to set the background
color to a brighter color while the foreground color should be of a darker color
like black (for easy viewing). Change the name to display as I am going to use it
later to write codes for the calculator.
• Now draw the command buttons that are necessary to operate a calculator. I
suggest you follow exactly what is shown in the image above.
Now run the project by pressing F5. If you are satisfied with the appearance, go
ahead and save the project.
Exercise 2
1. Write down the list of procedures which are associated with the Form object.
2. Write a program to display the sentence “I like Visual Basic”.
3. Write a program to compute the value of 1500+1000-450*10+300÷5
4. Using a value of 9 to X and a value of 13 to y, write a program to compute the
values of 2x+y, (x+y)÷2 and x2y
Lesson 3
Objectives
Before writing an event procedure for the control to response to a user's input,
you
have to set certain properties for the control to determine its appearance and
how it will work with the event procedure. You can set the properties of the
controls in the properties window or at runtime.
You can rename the form caption to any name that you like best. In the
properties window, the item which appears at the top part is the object currently
selected (in
Figure 3.1, the object selected is Form1).
At the bottom part, the items listed in the left column represent the names of
various properties associated with the selected object while the items listed in
the right column represent the states of the properties. Properties can be set by
highlighting the items in the right column and then changing them by typing or
selecting the options available. For example, in order to change the caption,
just highlight Form1 under the name Caption and change it to other names. You
may also alter the appearance of the form by setting it to 3D or flat. You can do
other things like changing the foreground and background color, changing the
font type and font size, enabling or disabling the minimize and maximize buttons
and etc.
You can also change the properties at runtime to give special effects such as
changing of colors, shape, animation effects and so on. For example the
following
code will change the form color to red every time the form is loaded.
Private Sub Form_Load()
Form1.Show
Form1.BackColor = &H000000FF&
End Sub
VB uses hexadecimal system to represent colors. You can check the color codes
in the properties windows which are shown under ForeColor and BackColor .
Shape1.Shape = 3
End Sub
This code will change the shape to a circle at runtime. Later you will learn how to
change the shapes randomly by using the RND function.
Note : I would like to stress that Learning how and when to set the objects'
properties is very important as it can help you to write a good program. So, I
advice you to spend a lot of time playing with the objects' properties.
I am not going into the details on how to set the properties. However, I would
like to
stress a few important points about setting up the properties.
You should set the Caption Property of a control clearly so that a user
knows
what to do with that command. For example, in the calculator program, as
all the captions of the command buttons such as +, - , MC, MR are
commonly found in an ordinary calculator, a user should have no problem
in manipulating the buttons.
A lot of programmers like to use a meaningful name for the Name Property
because it is easier for them to write and read the event procedure and
easier to debug or modify the programs later. However, it is not a must to
do that as long as you label your objects clearly and use comments in the
program whenever you feel necessary.
The text box is the standard control that is used to receive input from the user
as well
as to display the output. It can handle string (text) and numeric data but not
images or pictures. Strings in a text box can be converted to numeric data by
using the function Val(text). The following example illustrates a simple program
that processes the inputs from the user.
Example
In the above program, two text boxes are inserted into the form together with a
few labels. The two text boxes are used to accept inputs from the user and one
of the labels will be used to display the sum of two numbers that are entered
into the two text boxes. A command button is also programmed to calculate the
sum of the two numbers using the plus operator. The program creates a
variable, ‘sum’, to accept the summation of values from Textbox 1 and text box
2.
The procedure to calculate and display the output on the label is shown below.
b) The Label
The label is a very useful control for Visual Basic, as it is not only used to provide
instructions and guides to the users, it can also be used to display outputs. One
of its
most important properties is Caption. Using the syntax label.Caption, it can
display
text and numeric data. You can change its caption in the properties window and
also at runtime. Please refer to examples above for the usage of labels.
Statements
End Sub
The Picture Box is one of the controls that used to handle graphics. You can load
a
picture during the designing phase by clicking on the picture item in the
properties
window and select the picture from the selected folder. You can also load the
picture at runtime using the LoadPicture method. For example, this statement
will load the picture grape.gif into the picture box.
You will learn more about the picture box in future lessons. The image in the
picture
box is not resizable.
The Image Box is another control that handles images and pictures. It functions
almost identically to the picture box. However, there is one major difference, the
image in an Image Box is stretchable, which means it can be resized. This
feature is not available in the Picture Box. Similar to the Picture Box, the
LoadPicture method can also be used.
For example, this statement loads the picture grape.gif into the image box.
The function of the List Box is to present a list of items where the user can click
and
select items from the list. In order to add items to the list, we can use the
AddItem
method.
For example, if you wish to add a number of items to List box 1, you can key in
the following statements
Example
List1.AddItem “Lesson1”
List1.AddItem “Lesson2”
List1.AddItem “Lesson3”
List1.AddItem “Lesson4”
End Sub
The items in the list box can be identified by the ListIndex property, the value
of the
ListIndex for the first item is 0, the second item has a ListIndex 1, and the third
item
has a ListIndex 2 and so on
The function of the Combo Box is also to present a list of items where the user
can click and select the items from the list. However, the user needs to click on
the small
arrowhead on the right of the combo box to see the items which are presented
in a
drop-down list. In order to add items to the list, you can also use the AddItem
method.
For example, if you wish to add a number of items to Combo box 1, you can key
in the following statements
Example
The Check Box control lets the user select or unselect an option. When the
Check Box is checked, its value is set to 1 and when it is unchecked, the value is
set to 0. You can include the statements Check1.Value=1 to mark the Check Box
and Check1.Value=0 to unmark the Check Box, and use them to initiate certain
actions.
For example, the program will change the background color of the form to red
when the check box is
unchecked and it will change to blue when the check box is checked.
Note : You will learn about the conditional statement If….Then….Elseif in later
lessons. VbRed and vbBlue are color constants and BackColor is the background
color property of the form.
Example
The Option Box control also lets the user selects one of the choices. However,
two or
more Option Boxes must work together because as one of the Option Boxes is
selected, the other Option Boxes will be unselected. In fact, only one Option Box
can be selected at one time.
When an option box is selected, its value is set to “True” and when it is
unselected; its value is set to “False”.
In the following example, the shape control is placed in the form together with
six Option Boxes. When the user clicks on different option boxes, different
shapes will appear. The values of the shape control are 0, 1, and 2,3,4,5 which
will make it appear as a rectangle, a square, an oval shape, a rounded rectangle
and a rounded square respectively.
Example
The Drive ListBox is used to display a list of drives available in your computer.
When
you place this control into the form
and run the program, you will be
able to select
different drives from your computer
as shown below
k) The Directory List Box
The Directory List Box is used to display the list of directories or folders in a
selected
drive. When you place this control into the form and run the program, you will be
able to select different directories from a selected drive in your computer as
shown below
The File List Box is used to display the list of files in a selected directory or
folder.
When you place this control into the form and run the program, you will be able
to a list of files in a selected directory as shown below
You can coordinate the Drive List Box, the Directory List Box and the File List Box
to
search for the files you want. The procedure will be discussed in later lessons.
Exercise 3
1. Start a Visual Basic project and set the following properties of the form
Appearance=Flat
Caption=My First Program
Font=Times New Roman, Font Size=10
Forecolor=White
Backcolor=Blue
2. Insert six shape controls into the form and set their shape properties to
rectangle,
square, oval, circle, rounded rectangle, rounded square.
3. Insert two text boxes, one label and one command button, then write the
program to compute the product of two numbers that are entered into the
text
boxes and display the result in the label.
4. Insert a picture control in the form and load a picture from your computer
using
the LoadPicture Method.
5. Insert a List Box control into the form and add the 5 items into the List Box
using
the AddItem Method.