Professional Documents
Culture Documents
Event Driven
• In procedural applications, the application itself controls which
portion of code execute and in what sequence. Execution
starts with the first line of code and follows a predefined path
through the applications, calling procedures as needed.
• While in an event-driven application, different code sections
are executed in response to events. The sequence of these
events determines the sequence in which the code executes,
thus the path through the application’s code differs each time
the program runs.
• Your code can also trigger events during execution. For
example, changing the text in a textbox when the
program is running cause the text box’s change event to
occur. This will cause the code contained in the change
event to execute.
Interactive Development
• In Procedural languages, application development
process can be broken into 3 distinct steps:
i. Writing
ii. Compiling and
iii. Testing of codes.
However, VB uses an interactive approach to
development. In most languages, if you make a mistake
in writing your code, the error is caught by the compiler
when you start to compile your application but VB
interprets your code as you enter it, catching and
highlighting most syntax or spelling errors on the fly.
• Also, VB partially compiles the code as it is
entered. When you’re ready to run and test
your application, there is only a brief delay to
finish compiling. If the compiler finds an error,
it is highlighted in your code. You can fix the
error and continue compiling without having to
start over.
THE VB INTEGRATED DEVELOPMENT ENVIRONMENT
Form
Location
& Size
Window
Project
Explorer
Window
Form
Properties
Window
Properties
Help pane
Form
Layout
Window
The Form Window
The Form Window is where you design the forms
that make up your user interface. You can use
standard Windows techniques to change the size
and location of the form.
When you begin a new project, VB gives your new
form the default name Form1. As soon as you save
the file, you will give it a new (more meaningful)
name.
The Project Explorer Window
This Window holds the filenames for the files
included in your project. The window’s title bar
holds the name of your project (.vbp) file, which is
Project 1 by default until you save it with a new
name.
The Properties Window
This window is used to set the properties for the
objects in your project.
The Form Layout Window
The position of the form in the form layout window
determines the position of the form on the desktop
when execution of the project begins.
The Toolbox
The toolbox holds the tools you use to place
controls on a form. You may have more or different
tools in your toolbox, depending on the edition and
release of VB you installed.
The toolbox for VB 6.0 which is the version we’ll
use for this course is shown below.
Pointer Picture Box
Frame
Command Button
Check Box
Option Button
Combo Box
List Box
Horizontal Scroll Bar
Vertical Scroll Bar
Timer
Drive List Box
Folder List Box
File List Box
Shape
Line
Image
Data
OLE
The Main Visual Basic Window
The Main VB window holds the VB menu bar,
the toolbar and the form location and size
information.
The Toolbar
You can use the buttons on the toolbar as
shortcuts for frequently used operations. Each
button represents a command that can also be
selected from a menu.
The toolbar buttons are as show below:
Add Standard EXE Project
Add Form
Menu Editor
Open Project
Save Project
Cut
Copy
Paste
Find
Undo
Redo
Start
Break
End
Project Explorer
Properties Window
Form Layout Window
Object Browser
Toolbox
The Form Location and Size Information
The two small diagrams at the right end of the toolbar
show the position of the form on the screen along with
the size of the form.
Help
VB has an extensive Help feature that is greatly
changed and expanded in Version 6.0. Help includes the
MSDN library, which contains several books, technical
articles, and the MS Knowledge Base; a database of
frequently asked questions and their answers.
Design Time, Run Time and Break Time
VB has 3 distinct modes.
i. Design Time: While you are designing the user
interface and writing code, you are in design time.
ii. Run Time: When you are testing and running your
project, you are in run time.
iii. Break Time: If you get a run-time error or pause
project execution, you are in break time.
Module 2
Embarking On Application Development In VB
• To write a VB Project, the following three-steps processing are
necessary
i. Plan the User Interface: Draw a sketch of the screen the user will see
when running your project.
ii. Plan the Properties: For each object, put down the properties that you
intend to set or change during the design of the form.
iii. Plan the Basic code: Plan and determine which events require actions
to be taken and then make a step-by – step plan for those actions.
• You will create new windows called forms. Then you will use the toolbox
to add the various elements called controls.
• The program you will write follow a relatively new type of programming
called event-driven programming.
• Microsoft refers to VB as an event-driven programming language.
• In the event – driven model, programs are no longer procedural; they do
not follow a sequential logic. You as the programmer do not take control
and determine the sequence of execution. Instead, the user can press
keys and click on various buttons and boxes in a window.
• Each user action can cause an event to occur, which triggers a Basic
procedure that you have written.
• E.g. the user clicks on a command button labeled Calculate. The clicking
causes the button’s Click event to occur, and the program automatically
jumps to a procedure you have written to do the calculation.
METHODS
• Actions associated with objects are called methods. Methods are the
verbs of Object-Oriented Programming. Some typical methods are
Move, Print, Resize, Clear.
Visual Basic Projects file Extension
Each VB project consist of at least two (2) files and usually more files.
a.) The Project file with extension .vbp: This is a small text file that
holds the name of other files in the Project, as well as some
information about the VB environment.
b.) Form file with extension .frm: Your project may have one or more
form files. A form file holds a description of all objects and their
properties for the form, as well as the Basic code written to respond to
the various events.
c.) Basic Statements file with extension .bas: These hold Basic
statements that can be accessed from any forms especially where
more than one form is used.
d.) Custom Controls file with extension .ocx: These are used to handle
controls which are not part of the standard controls.
e.) Others with extension .vbw: This is automatically added by VB to
hold information about each of the projects forms.
Procedures in Visual Basic
• You can simplify programming tasks by breaking programs into smaller
logical components. These components called Procedure can then
become building blocks that let you enhance and extend VB.
• Procedures are useful for condensing repeated or shared tasks, such as
frequently used calculations, text and control manipulation and database
operations.
There are two major benefits of programming with procedures:
i. Procedures allow you to break your programs into discrete logical units
each of which you can debug more easily than an entire program without
procedures.
ii. Procedures used in one program can act as building blocks for other
programs, usually with little or no modification.
There are 3 types of procedures used in VB.
i. Sub-Procedures: It does not return a value.
ii. Function Procedures: Returns a value.
iii. Property procedures: Can return and assign values and set references to
objects.
Sub Procedure: The Syntax for a Sub Procedure is
[Private|Public] [Static] Sub Procedurename (arguments)
Statement
End Sub
Each time the Procedure is called, the statements between Sub and
End Sub are executed.
Sub procedures can be placed in standard modules, class module and form
module.
In VB, there 2 types of Sub-Procedures
i. General Procedures.
ii. Event Procedures.
• A General Procedure tells the application how to perform a specific task.
Once a general procedure is defined, It must be specifically invoked by
the application.
• Meanwhile, an event procedure remains idle until called upon to respond
to events caused by the user or triggered by the system.
• A good programming strategy is to put common statements in a general
procedure and have your event procedures call it.
• When an object in VB recognizes that an event has occurred, it
automatically invokes the event procedure using the name corresponding
to the event. Event procedures are attached to forms, controls and menu
commands.
Function Procedure:
• VB includes built-in or intrinsic functions, like sqr, cos or chr. However, you
can use the function statement to write your own function procedures.
The syntax for a Function Procedure is:
[Private|Public] [Static] Function Procedurename (arguments)[ As type]
Statement.
End Function.
Unlike a Sub-Procedure, a Function procedure can return a value to the
calling procedure.
E.g. Write a function to Calculate the area of a circle given it’s radius.
SOME STATEMENTS IN VISUAL BASIC
Some unique statements in VB are:
Remark Statement
• Remarks statement sometimes called comments are used for project
documentation only. They are non-executable and have no effects when
the program is run. It only makes a program more readable and
understandable. VB remarks begin with an apostrophe.
e.g.
‘This is a sample program written by 200L
‘ College of Science and Technology
‘ Covenant University
• Memory locations that hold values that can change during project
execution are called variables; locations that hold data that cannot
change during execution are called constants.
• When a variable or a named constant is declared, an identifier is used
to reserve memory location for it.
Variable Declaration
• The most common way of declaring variables is by the use of Dim
statement.
• The general format is
Dim identifier [As Datatype]
Where a Datatype is missed, a variant is implied.
Data Types
• The data type of a variable or constant indicate what type of information
will be stored in the allocated memory space: The default data type is
variant which allow any kind of data but usually requires more memory
and operate less quickly than other data types.
Data Types Use for Number of Prefix
Bytes of
Memory
Allocated
Date
Date 8 dtm
Double Double-Precision 8 Dbl
floating point with
14 digits of
accuracy
Whole numbers in
Integer the range of 2 Int
32,768 to -32,767
vnt
Covert from one Numbers- 16
Variant type to another as bytes Character-
needed. 22 bytes plus 1
byte for each
character in the
string.
Class Exercise
• Write a VB code that opens the Save As Dialog Box and Use
this dialog box to save the picture on the Picture Box in the
previous example in a folder named; ”GEC 225 Codes” on
your C drive.
Assignment
1. Use VB to design your School ID Card and write a code to
print the ID card using Print Dialog Box.
2. Submit the Printed ID Card with the Source Code on Friday
27th April, 2007.