Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Second Lecture

Programming in Visual Basic


Visual Basic Language firstly appeared on 1991 and since that time different
versions based on modifications have shown for using. We are going to study Visual Basic
version 6.0. Visual Basic Language is considered from Windows Programming Language,
as it is utilized to create programs can work under windows operating system. You have to
be very familiar with Windows operating system. Visual Basic considered as driven event
language which means; the language that works based on dividing the main program into
sub programs and will be executed when an event took place, like pressing one of the
buttons or moving the mouse.Visual Basic programs display a Windows style screen (called a
form) with boxes into which users type (and edit) information and buttons that they click to initiate
actions.The boxes and buttons are referred to as controls. Forms and controls are called
objects.Visual Basic is a tool that allows you to develop Windows (Graphic User Interface -

GUI) applications. The applications have a familiar appearance to the user. Visual Basic
implements graphical user interface that allows the use of graphics for different
applications. It provides visual interactive windows with user, like Dialogue box for
(color, font ...), Input box, and Output box. Also it is able to create menu to simplify user
application. So that when you start a program you have to select which kind of events and
what will be the respond for them.

The Concept of Event Driven Programming

P a g e 11 | 32
Event: It is what the user makes as actions on the program, like pressing a button or
selecting item from a list, or moving mouse, or clicking mouse button. When events
occurred, windows take this event to the executed program and giving it a message about
the nature of this event that took place on this program, so that the program will analyze
this message and take the right action for this event after the program complete, the
control came back to the operating system. It is not necessary that the program has to
respond to any event, for example moving the mouse on the screen of the program is
considered as an event, but that does not mean the program has to respond to that event,
unless the program is drawing program. Event driving Programming leads to divide the
program into many parts and each one of them will respond to a certain event, and we start
making the code for each part of the program. Visual Basic make this task easy to us, as it
is automatically dividing the program into parts and each part called action (sub routine)
and this action is controlling in a certain action for a certain tool.

To Run & Exit from Visual Basic on user computer


Start>programs>Microsoft Visual Studio 6.0>Microsoft Visual Basic 6.0.
To exit from Visual Basic and return to Windows is like exit from most Windows
applications. There are three ways to close the Visual Basic as stated below.
1- Click on close button icon that appears in the upper-left corner of the screen.
2- Press Alt+F4
3- Select File >Exit.

The Importance of Visual Basic Program


Languages like Basic and Pascal depend on variables and procedures to build the
applications .This is why it is called procedural languages. Thenew approach is called
object programming for visual programs like VisualBasic and Visual C++ and others. In
this programming approach everything(form, command buttons, controls) is an object.
The reasons for implementing Visual Basic program are listed as follows:
1- It uses integrated development environment (IDE) which is easier forthe user to
minimize code writing.
2- All visual programs follow the same concepts, therefore the user willbecome more
familiar with visual approach for other visual languages.
3- It provides Input box and Output box as an interactive windows withuser.

P a g e 12 | 32
4- It is able to connect to Internet, and to call Explorer.

Visual Basic Environment


In this lecture you will explore the Visual Basic environment. A unique
programming environment that at the beginning can be a bit intimidating, but once you
have completed this topic, you will find that it is actually a rich and user friendly
environment. Let’s use start with the first dialog box that you will encounter once you
start Visual Basic. Fig. (2-1) shows the Visual Basic’s new project dialog box. Using this
dialog box you will initiate your first project in Visual Basic. As you can this dialog box
has three tabs. The new tab lets you initiate a new application. In this course you will
mostly create Standard.EXE(EXE means executable program) applications, i.e.
standalone windows programs.
Visual Basic also allows you to create other applications such as ActiveX control
and ActiveX DLL (Dynamic Link Libraries – sharable subprograms used by other
programs) or other applications as viewed in this dialog box.

Fig. (2-1): Visual Basic New Dialog Box.

P a g e 13 | 32
There are two other tab. Existing tab lets you open existing Visual Basic projects to
edit or modify, while the recent tab provides you with a list of most recently created or
edited Visual Basic projects. To start a new project, click on the Standard.EXE icon and
then click on the open button. The windows shown in Fig. (2-2) will open. We now
explore the components of this windows one by one. The menu bar is at the very top.
Menu bar contains most of the utilities that are available to you in the Visual Basic
environment. Such as File, Edit, View, etc. Click on each of these items to see the
available utilities.
Under the Menu bar is the Toolbar. Toolbar contains some of the most often used
items from the Menu bar in small icons. Move the mouse pointer on each icon and leave it
for a mount and the function of the icon will appear in a small sidebar.
The Toolbox in the Visual Basic environment contains the controls (also refer to as
object) that are most often used in developing applications. Throughout this course you
will learn how to use all these controls.

P a g e 14 | 32
Fig. (2-2): New Project Environment.

Fig. (2-3): Visual Basic Environment Menu bar.

Fig. (2-4): Visual Basic Environment Toolbars.

Fig. (2-5): Visual Basic Environment Toolbox.


In the following table the controls in the Toolbox will be introduced one at a time.
In the following lectures these controls will be used in different applications and their
properties will be explored. Pointer it does not draw a control. Pointer lets you select,
resize or move a control already on a graphics and print methods. Label displays text that
can’t be changed, like caption under a graphic. It can be changed by program codes.

P a g e 15 | 32
Table 2-1: Most frequently used controls in the Visual Basic environment
PictureBox You use a picture box to display text or graphics output.

Label You place a label to the left of a text box to tell the user
what type of information to enter into the text box. You
also use labels to display output.
TextBox Holds text that the user can either enter or change or text
generated by application.

Frame Allow you to create a visual or functional grouping for


controls. Draw the frame, and then draw controls inside
the frame to form a group.
CommandButton Creates a button the user can choose (click) to carry out a
sequence of instructions.

CheckBox To choose between yes ‘no, true ‘false, and include


‘exclude or multiple choice when more than one item may
be chosen.
OptionButton This control is used to select one item from a group of
items.

ComboBox Combines the features of list box and text box. The user
can either choose an item from the list or enter a value in
the text box.
ListBox Display a list of items from which the user can choose
one.

HScrollBar Provides a graphical tool for moving through lists or


selecting data ranges.

VScrollBar Provides a graphical tool for moving through lists or


selecting data ranges.

Timer Used to activate a specific event at set interval. This


control is invisible at run time.

DriveListBox Display valid disk drives at run time.

DirListBox Display directories and path at run time.

FileListBox Displays a list of files at run time.

P a g e 16 | 32
Shape Used to draw a variety of shapes such as a rectangle,
square, rounded square, oval, or circle, etc.

Line Used to draw a variety of line styles on your form at


design time.

Image Displays a graphical image from a bitmap, icon, metafile


as well as JPG or GIF files. It is decorative and uses fewer
resources than a PictureBox.
Data Provides access to data in databases through bound
controls on your form.

OLE Allow you to link and embed objects from other


applications in your visual basic application.

There are three other windows that appear in the Visual Basic environment. They
are Project window, Properties window and the Layout window. The Project window
provides an explorer type view of all the forms and modules in the project, while
Properties window a list of all the properties available for each control with their
corresponding value.
Layout window shows the relative location of the forms on the screen. As you
change the location and size of the forms the changes are reflected in the Layout window.
Fig. (2-6) shows Visual Basic’s Project Window, Properties window and Form Layout
window. You can change the location of the application window (the form) by moving its
location in the Form Layout window. To change of the Form in the Form Layout window
simply click on it (where it’s written Form1) and drag it to and location in the monitor’s
picture.

P a g e 17 | 32
Fig. (2-6): Visual Basic’s Project Window, Properties window and Form Layout window.

Form window is the main window in the Visual Basic environment. Shown in Fig.
(2-7), this is the window within which the application interface is developed. You develop
the application interface by placing controls in the Form.

Handles

Fig. (2-7): Visual Basic’s Form window.


You can change the size of the Form by clicking on the handles and dragging them.
Once the controls are placed in this window and their properties are assigned in the
Properties window it is time to attach codes to each control as needed. Attaching code to
the control means writing the sequence of instructions that pertains to each control. In
order to write the code for each control you most first bring up the code editing window
for that control. You can do that by double clicking on the control. If you wish to attach
code to a Form, you should double click on the Form itself.

Procedure
Control (Object)

P a g e 18 | 32
Fig. (2-8): Visual Basic’s Code Editing Window.

P a g e 19 | 32
Once the code edit window appears, the control that the code is being written for is
shown in the top left Combo Box and the event procedure is shown in the top right Combo
Box. Visual Basic is an event driven programming language. This simply means that the
applications that are developed using Visual Basic, much like any other windows
applications, will act upon the user’s actions (event procedures) such as Click, Double
Click, Drag Drop, Mouse Down, etc. Whenever you want the control in your application
to respond to an event, you put the instructions in the appropriate event procedure. Of
course the user does not initiate all the events. Sometimes events are consequence of other
events. For example as you start an application the main Form is loaded in the memory
and shows up on the screen. This is a Form-Load event, which simply means that the Load
event procedure is activate on the Form control. Using the arrow in the left Combo Box
will show all the controls that are used in the application and clicking on the arrow of the
right Combo Box shows all the available procedure for the control that is shown on the
left Combo Box.

Control Properties
Every control in a Visual Basic application has certain characteristics called
properties. These properties dictate the way a control looks, size, color, and screen
location amount other things. When you place a control in your application (either by drag
and drop or by double click on it) the property window will display the properties
corresponding to that control. There is a default value for each property. You may change
these values to fit the purposes of your application. Once a control is placed on a Form,
Visual Basic automatically will issue a name for that control. This is the name that the
control will be known by, to the rest of the application. When you write codes for that
control you notice that the name that Visual Basic has given to this control appears in the
top left Combo Box of the code edit window. This name can be changed, just like any
other property that appears in the property window. Fig (2-9) shows a Form that has a
Command Button control placed in it.

P a g e 20 | 32
Once a control like the Command Button control is placed on the Form, it has
“Command1” written on it. This is called the Caption. This may become a little confusing since
the Name property of the control that is also “Command1”. In other word, both the Name and
Caption (what appears in the Command Button control) properties have the same value. To
distinguish between them, the Caption on the Command Button control was changed to “Click on
me” and the Font property was changed too.

Fig. (2-9): Properties of the Command Button.

Another noteworthy item is that when there are more than one control present in the
application during the design time, the properties of the control that is selected will appear
in the Properties window. Note the handles around the Command Button control in Fig.
(2-9), these handles are indicators that Command Button control has been selected.

P a g e 21 | 32

You might also like