Visual Basic PDF Eng
Visual Basic PDF Eng
Introduction to VB
The evolution of VB took place while developing & designing windows based
application. Programming windows based application used to require hours of
hard work for creating interface only. Visual Basic was introduced in the year
1991. Basic idea of introducing VB to the world was to provide a rapid
application development environment to the developers. This environment
was facilitated so that developers could put in all their concentration in
Application’s design rather than how to design. Where other languages take
hours of work for creating an interface, the same application can be created
in few hours using Visual Basic.
1
Semester VI Visual Basic
2 Event driven programming- When you program in VB, you must first
decide how the application interact with user. In other words you must
decide how each control reacts to user action such as mouse click etc. and
you must program for the reaction. This is called event driven programming
because the application does actions on the basis of events caused by the
user determining the flow the program execution.
Operating System:
Windows 95 or above version of windows family is required for running VB
5.0/6.0.
Visual Basic terminology is very vast. It is difficult to select few terms to
explain because all terms are quite often used & have their own importance.
2
Semester VI Visual Basic
some class. These are called as an object as soon as they are included in the
project.
Properties
A property is a changeable characteristic of an object e.g. color, size &
caption, etc. various valid values can be set to properties of an object.
Properties are named using nouns e.g. Text, Back color, etc.
Properties can be categorized into 3 categories:
Design-time
Run-time
Design & Run-time
Methods
Methods are the actions that can be performed on any object or by the
object. They may or may not change the state of the object. Generally they
are named using verbs, such as open, hide, move, etc. properties of an
object can be accessed in any order while methods must usually be executed
in a specific order.
Events
Events in VB can be called as the happenings with the objects while an
application is running. These happenings can be caused due to the user’s
action on keyboard, mouse or even by the application itself
Forms
Form in VB is actually the workspace where you design the visual layout of
the form & the controls that lie on it. This is actually the window of your
application in run-time. VB IDE displays forms & code window separately.
The code window is actually the space where you write your codes & can
visually classify between different procedure & functions.
Functions:
Procedures:
is important to begin the process of learning this language.
Constant:
Variable that do not change their values during program execution are called
constant. Constants evaluate the same value each time. By the followings
reasons constant are preferred over variables.
Syntax - Const <Constant Name> [As type] = <value>
e.g. Const dimension as single = 22.41
3
Semester VI Visual Basic
Print statement:
It is used to display information within currently active form starting from the
upper left corner. This statement consists of a key word print followed by a
list of out put items. The out put item can be numeric, string, constant or
expression
Print with tab: it is used to make columns in tabular forms. Tab allows the
computer to insert desert desired no of spaces before printing a matter.
Num1=1000.234
Num2=10.2364
Result =num1\num2
4
Semester VI Visual Basic
^-exponent—2^3=8
Assignment -- =
Logical operator –
And operator 1<2 and 3>4 =>0
Or operator 1>2 or 2<4 => 1
Not operator not 2<4 => 0
Concatenation & and + are used to join two string expression.
A= ―34‖
B= ‖54‖
Print A+B
Or
Print A&B
5
Semester VI Visual Basic
Basic controls:
Labels:
Label control is used to display the information which is read only. A label
control is similar to text box in the sense that both display text. The only
difference being that label control provides a read-only interface where as
you can change text in the textbox.
Textbox:
Generally almost each and every application uses a textbox to accept an
input from the user. Textbox has all the required properties and events that
can be used in an application. The textbox control has the text property as
the default property. You can add scrollbars to it, set font for the text, set its
back color. Multiline property enables you to allow typing in multi lines.
6
Semester VI Visual Basic
Timer control:
This control is similar to the time bomb which explodes after a certain time.
Only difference is that it explodes (execute the code) continuously with a
fixed time gap unless it is stopped. Timer controls should not be overused, as
a window restricts all the applications running at one time to 16 items.
There are 26 basic tools (including the above stated) in the tool box:
Pointer
Picturebox
Label
Textbox
Frame
Command button
Checkbox
Option button
Combobox
Listbox
Hscrollbar (horizontal scroll bar)
Vscrollbar (vertical scroll bar)
Timer
Drivelistbox
Dirlistbox
Filelistbox
Shape
Line
Image
Data
Commondialog
Grid
7
Semester VI Visual Basic
Showing Forms:
To show a form the show method is used. If a form is loaded but not shown
the show method brings the specified forms on the top of every open
window. If the form is not loaded show method loads and then displays it.
8
Semester VI Visual Basic
A model form takes control of the application and could not load the
application process unless the form is closed. A model form must have close
button something like that to close it.
Hiding Forms – If your application usage many forms you may want to hide
some of them to make space on the desktop for other.
Syntax <Form Name>.Hide
(me.hide)
Scope of Variable- The variables also has a scope. The Scope of a variable is
the sections of the application that can see any manipulation of the variables.
The Scope of a variable determines the section of an application where the
variable can be use. A variable can be defined with following any of the three
scopes.
Form Wise/Module Wise- These variables are available to all the functions
present in a form or module. They also considered as private variable.
Global- Variable that can be accessed by the entire application comes under
global Scope. To declare a global Scope variable use public keyword in place
of dim.
e.g.- Public num as integer.
These are defined in general declaration area of the code. A run time error is
generated if you try to redefine public variable in a procedure.
User defined data type- VB allows you to create your own data type
containing one or more elements such as structure.
Syntax: type <Variable Name>
<Var 1> as type
<Var 2> as type.
End type.
9
Semester VI Visual Basic
If-then-else:
VB provides if then else statements for decision making. E.g. If condition is
true then if part execute otherwise else part will be executed. If the test fails
and else part is not mentioned then the processing skips to the next
statement.
Example: if x =0 then msgbox ―zero‖ else msgbox ―non-zero‖
More often multiple statements’ are to be processed if a certain condition is
true or false. All such statements are thus blocked in if…end if statements.
Multiple conditions can be checked in one if statement using AND keyword.
One out of many conditions can be checked for true using OR keyword.
You can also use nested if structure i.e. if-then block within if – then blocks.
Nested if statements can be replaced by using if – else if – end if structure.
IF condition then
[Statement1]
[Statement2]
.
.
.
End If
IF-ELSE-IF Structure
If [condition1] Then
[Statement1]
[Statement2]
.
.
.
Else If [Condition2]
[Statement1]
[Statement2]
.
.
.
End If
Immediate if:
The iif() function provides immediate if for quick decisions. The iif() function
returns one part out of two, after evaluating the given expression. The entire
expression consists of two parts the if part and else part.
Example:
Result = iif div >=60, ―first‖, ―second‖
10
Semester VI Visual Basic
11
Semester VI Visual Basic
For –Next- Unlike do…….loop, the for …..next loops required that you have to
vow how many times the statements in the loop will be executed.
The for next loop user go a variable that in creases & decreases in value
during lath repetition of loop
for <variable> = <start> to <end> statement
Determinate loops:
Repeat the operation for a fixed number of times.
Indeterminate loops:
Continues until you reach a specific predetermined goal, or continue until
certain initial conditions have changed.
For—next:
Using this loop structure enables the program to execute a block of
statements a fixed number of times. At the beginning of the loop, specify the
starting value and ending value for the counter. The program executes till
the counter reaches the ending value.
e.g.
for x = 1 to 10
text1.text = x
next
Do—loops:
This loop structure enables the program to execute a block of statements an
indefinite number of times based on a condition. Each time the code loops,
the condition is tested again. When the condition finally becomes false, the
loop terminates.
Do-while loop:
This executes a code indefinitely till some condition is True or False.
General syntax:
Do while some condition is true
Execute these statements
12
Semester VI Visual Basic
Loop
e.g.
var = 10
Do while var <> 0
print ―it is rainging‖
var = var + 1
Loop
While-wend statements:
Execute a series of statements as long as a given condition is true.
Syntax:
While condition
[Statement]
Wend
Example:
All the properties of textbox can be referenced as
With text1
.text = ―howard‖
.height=1400
.width=1800
.forecolor=rgb(0,256,0)
End with
Arrays:
Variables that can hold more than one values, are called as arrays. Generally
arrays hold the values of similar data types but can hold different values of
different data types if they’re declared as variant type. The lowest subscript
of arrays depends upon the option base statement. However it is 0 by
13
Semester VI Visual Basic
default. In such a case the last element of an array of four elements will have
index number 3. Arrays can be declared using all the keywords that can be
used to declare variables.
Syntax:
Dim arrayname(subscript) as type
Example:
Dim name(10) as integer
Dynamic array:
This has been noticed that generally you are not aware of the total number
of elements to make an array while programming. For example your
application stores the records of employees, but the number of employee is
unknown. In such a case you must declare an empty array. An array that
starts life with no elements is called as dynamic array.
Array or lists whose size can be changed on the fly are known as dynamic
arrays or dynamic lists. You can resize an array using redim statement. VB6
has introduced the new concept of dynamically resizing the target array to
the size of the source array. To declare a dynamic array you must ignore the
size of the array in declaration statement.
Dim sizeunknown () as integer
The size of the array is not specified, and thus sizeunknown will be a dynamic
array whose size can be changed later in the program.
Re-dimensioning an array:
The keyword used to re-dimension an array is Redim. The Redim statement
should be accompanied with preserve keyword in order to retain the initial
values of the array.
Example:
Redim preserve sizeunknown(10)
This re-dimension feature of VB can help you to develop a link-list without
the use of pointers.
Collection:
14
Semester VI Visual Basic
The new keyword tells us to create a new collection. The collection objects
have three methods (add method, remove method, item method) & one
property (count property).
Adding to a collection: The add method is used to add new items to the
collection
<Collection Name>. add <value>, key [before/after]
15
Semester VI Visual Basic
([<arguments>])
Body of procedure
End Sub.
Visual Basic supports two types if subprocedure
1) Event Subprocedure 2) General Subprocedure
Event Subprocedure: These procedure are attached with event you can
write event write procedure for controls and for forms.
Event procedure also called event handless. When an object in VB recognized
that event it automatically invokes the event procedure corresponding to that
event. It is a self contained place of code that is executed when needed.
Syntax :
Private Sub < Control Name>_<Evenet Name> ([<Argument List>])
Body of procedure
End Sub.
Access Specifier:
There are two access specifiers called private & public. These specifiers are
used to determine the scope of subprocedure. By default, subprocedures are
public means they can be called from any where in the application.
Static:
This keyword is used to ration value during the life time of subroutine
irrespective of how many times that procedure is called.
Sub:
This keyword tells the compiler to define a procedure.
Procedure Name:
Name of the procedure follows following rules
First character should be any alphabet or under score only.
Special characters like + - * / . % spaces etc are not allowed
anywhere.
Procedure name should be indicative.
Procedure name must be preceded by ().
Arguments:
It includes list of arguments based on the events when a procedure is called.
16
Semester VI Visual Basic
Function Procedure:
A function is similar to sub procedure but it returns a result.
The function statement are written function ………..End function.
Creating Procedure:
A procedure can be created by just typing it in the code window you can also
create procedure by
tools >> add procedure
Statement:
Code written within or outside of the procedure or function without comment
character is called a statement.
Input box Function: Input box used when user asks for input by clicking a
button
Syntax- input box (prompt [title]
[default] [x position] [y position]
17