You are on page 1of 33

GEC 225

(APPLIED COMPUTER PROGRAMMING WITH VISUAL


BASIC)

COLLEGE OF SCIENCE AND TECHNOLOGY


COVENANT UNIVERSITY,CANAAN LAND,
OTA, NIGERIA
LECTURE MODULEs
Module 1 Module 3
• Differences between QBasic • Writing to and Reading from
and Visual Basic files e.g. Notepad, Ms Word.
• Steps in Creating Application • Writing to and Reading from
Database: Access.
• Visual Basic Integrated
Development Environment. • Writing to and Reading from
text and databases over the
Module 2
network.
• Embarking On Application
• Printing in VB.
Development in Visual Basic
Module 4
• Building Blocks for Application
Development • Multimedia with VB: Sound,
Music, Video
• Menu Design • Animation with VB.
• Procedures: (i) Function (ii) • Calling other Applications from
Subroutines (iii) Event VB(Shell Execute) .html, .pdf,
Procedure .doc files
• Opening Pictures and Saving
Pictures to Folder
Module 1
Differences between QBasic and Visual
Basic
QBasic VB
• Procedural • Object-Oriented
• Command line • Graphical User Interface
• DOS Based • Windows Based
• Non-Interactive • Interactive Development
Development
i. What is a Window?
A Window is a rectangular region with its own
boundaries. Example of Windows are:
An Explorer Window
Document Window within your Word Processing
Program
A dialog box that pops up to remind you of an
appointment
Other examples of windows are; A command
• The Microsoft Windows Operating System manages all of
these many windows by assigning each one a unique ID
number called (Window handle or hWnd). The system
continually monitors each of these windows for signs of
activity or events .
• Events can occur through user actions such as a mouse click or
a key press, through programmatic control or even as a result
of another windows actions.
• Many of these messages are handled automatically by VB
while some are exposed as Event- Procedures for your
convenient manipulation

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

• The VB Integrated Development Environment is


where you create and test your projects. The figure
below shows the various windows in the VB IDE.
Note that each window can be moved, resized,
opened and closed.
Main VB Window Toolbox Menu bar Tool bar Form Window

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

Label Text 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.

The Windows GUI


• Ms Windows uses a Graphical User Interface or GUI. The GUI
defines how the various elements look and function.

• As a VB programmer, you have available a toolbox of these elements.

• 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.

The Object Model


In VB, you will work with Objects, which have Properties and Methods.
OBJECTS
• Think of an object as a thing or noun. Examples of objects are Forms
and Controls.

• Forms are the windows and dialog boxes you place on the screen.
• Controls are the elements you place inside a form such as text boxes,
command buttons and List boxes.
PROPERTIES
• Properties tell something about an object such as its name, color, size,
location or how it will behave. Properties are adjective that describes
objects.
• When you refer to a property, you first name the object, add a period
and then name the property. For example, refer to the Caption property
of a form called Form1 as Form1.Caption

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

The Assignment Statement


• The assignment statements assign a value to a property or a variable.
• Assignment statement work from right to left, assigning the value on the
right side of the equal sign to the property named on the left side of the
equal sign.
e.g. lblTitle.Caption = “Covenant University”
intNum1=Val(txtNum1.text)
The End Statement
The End statement stops execution of a project
e.g.
End

Data ----- Variables and Constants


Consider an assignment statement;
intNum1=50
• The memory location is called intNum1 and the content is 50.

• 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

Boolean True or False Values 2 bln

Byte A single ANSI character (code 1


0 to 255)

Currency Decimal Fraction such as 8 Cur


dollar.

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

Long Large whole 4 Lng


numbers.

Single Single-precision 4 sng


floating number
with six digit of
accuracy.
String Alphanumeric 10 bytes plus 1 str
data; letters, byte for each
digits and other character in the
characters. string.

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.

Examples of Variable Declarations


Dim strStudentName As String
Dim curPrice As Currency
Dim sgnCurrent As Single
Dim intVoltage As Integer
Creating Menu With Menu Editor
• You can use the Menu Editor to create new menus and
menu bars, add new commands to existing menu, replace
existing menu commands with your own commands and
change and delete existing menus and menu bars.
• The two, most important properties for menu controls are:
i. Name: This is the name you use to reference the menu
control from code.
ii. Caption: This is the text that appears on the control.

Separating Menu Items


A Separator bar is displayed as a horizontal line between
items on a menu. On a menu with many items, you can use
a separator bar to divide items into logical groups
Assigning Access Keys and Shortcut Keys
• You can improve keyboard access to menu commands by
defining access keys and shortcut keys.
• Access Keys: Access keys allow the user to open a menu
by pressing the ALT key and typing a designated letter.
Once a menu is open, the user can choose a control by
pressing the Letter (the access key) assigned to it. E.g.
Alt + E might open the Edit menu, and P might select the
Paste menu item. An access key assignment appears as an
underlined letter in the menu control’s caption.
• Shortcut Keys: Shortcut keys run a menu item-immediately
when pressed. Frequently used menu items may be
assigned a keyboard shortcut, which provide shortcut key
assignments include function key and control key
combinations. Such as CTRL + F1 or CTRL + A. They
appear on the menu to the right of the corresponding menu
item.
Examples:
1. A Procedure for Opening Open Dialog Box
Private Sub mnuFileOpen_Click ()
' Set filters. CommonDialog1.Filter = "All Files (*.*)|*.*|Text _
Files (*.txt)|*.txt|Batch Files (*.bat)|*.bat"
' Specify default filter.
CommonDialog1.FilterIndex = 2
' Display the Open dialog box. CommonDialog1.ShowOpen
' Call the open file procedure. OpenFile
(CommonDialog1.FileName)
End Sub

2. A Procedure for Opening Picture and Putting the Picture in a


Picture Box
Private Sub OpenFile()
'Assign An Existing Picture on the System to the Picture Box
Picture1.Picture = LoadPicture(CommonDialog1.FileName)
MsgBox "Picture Successfully Assigned To The Picture Box", vbOKOnly,
"GEC 225 Sample Code"
End Sub

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.

You might also like