Visual Basic
1)Introduction to VB
2) Programming –variable declaration, Types ,control flow
statements ,Looping statements
3) Event Driven Programming
4) Common Methods
5) Mouse events
6) Keyboard events
Visual Basic(VB 6.0) is OBL(Object based Language)-It has 2 parts
1)Project part-Extension of project is .vbp
2)Form part-Extension of form is .frm
Elements of the user interface-Use of controls-
study from textbook Page no.99 and 100.
Picture box, Label, Textbox, Frame, Command button, Check box
,Option button, Combo box, List box, Horizontal and vertical scrollbar,
Timer, Shape, Line and OLE.
VB Programming:
Variables: In VB variables store values during a program’s execution. A
variable has a name and value. variables must be declared in advance
for the compilers.
Declaring Variables- 2 types of declaring variables
1) Explicit Declaration-It is used to declare a variable.Use the
“Dim” statement followed by the variable name and type as
follows.
Ex.Dim a as integer
2) Implicit Declaration- The variable type is variant , the generic
data type that can accommodate all other data types VB adjusts
its type according to the value you assign to it.
Ex. Dim a,b
Variable Naming Conventions or Rules of variables
A variable name
1) Must begin with a letter
2) Can’t contain an embedded period(.) or any other
type of declaration of characters
3) Must not exceed 255 characters
4) Must be unique within its scope
Data Types of variables
1) Numeric Integer(4 bytes,both +ve / -ve values,no decimal)
2) String Single(4 bytes,with decimal point)
3) Boolean Long(8 bytes,no decimal point)
4) Date Double(8 bytes,with decimal point)
5) Object Currency(it is giving exact value)
6) Variant
2) String- It is used for data values that are made up of
ordered sequences of characters, such as “hello world”.
3)Boolean- It contains either of 2 states like True/False,
Yes/No, On/Off etc.
4) Date-It stores the calendar date either mm/dd/yy or
dd/mm/yy.
5) Object- An object is a combination of code and data that
can be treated as a unit.
6) Variant- It is a special data type that can contain any kind
of above 5 data types in VB
Control flow statements
There are 3 types of control flow statements :
1) If……Then
2) If……then ….Else
3) Select case
1) If……Then : The Syntax is given below:
If (condition) then
statement1
End If
If the condition is true statement1 is executed and if the condition is false state after end if is
executed.
Ex.
If (a=b) then
Msgbox “both the values are equal”
End if
1) If……Then ….else: The Syntax is given below:
If (condition) then
statement1
Else
Statement 2
End If
Meaning : If condition is true then statement1 is executed,
if condition is false then the statement2 is executed.
Eg.
If(a>b) then
Msgbox a “is greater”
Else
Msgbox b “is greater”
End if
Nested IF
If……then …..Elseif
Syntax:
If((condition1) and (condition2)) then
Statement-1
Else if ((condition3) and (condition4)) then
Statement-2
Else
Statement-3
End if
Meaning: If condition1 and condition2 is true then statement-1 is
executed, if condition1 and condition2 is false then condition3 and
condition4 is tested and if it is true then statement-2 is executed and
if the condition3 and condition4 is false then statement-3 is executed.
If any of the condition is not satisfied then statement 3 is executed.
Eg. If((a>b)and(a>c)) then
Msgbox a “is greater”
Elseif((b>a)and(b>c)) then
Msgbox b “is greater”
Else
Msgbox c “is greater”
End if
Select Case
The select case structure tests a single expression , which is
evaluated once at the top of the structure. The result of the
test is then compared with several values, and if it matches
with one of them then, the corresponding block of
statements is executed.
Select Case (Expression / Value)
Case value 1: Statement block1
Case value 2: Statement block2
.
.
Case else statement block
End select
Eg.
Select case day
Case 1: msgbox “monday”
Case 2: msgbox “Tuesday”
Case 3: msgbox “Wednesday”
Case 4: msgbox “Thursday”
Case 5: msgbox “Friday”
Case 6: msgbox “Saturday”
Case 7: msgbox “Sunday”
Case else
Msgbox “It is not a week day”
End Select
Loop Statements
Loop statements are the statements which allows to execute
one or more lines of code repetitively.
There are 3 types of looping statements
1) While…..wend
2) Do----Loop
3) For…..Next
1) while----wend
Syntax : While condition
statement –block
wend
As long as the condition is true the statement block is executed
and if the condition is false then the statement after wend is
executed.
Eg. s=0,i=1
While(i<=5)
s=s+i
i=i+1
wend
2) Do----Loop
Syntax : Do While condition
statement –block
Loop
As long as the condition is true the statement block is executed
and if the condition is false then the statement after Loop is
executed.
Eg. s=0,i=1
Do While(i<=5)
s=s+i
i=i+1
Loop
3) For….next
Syntax : For counter=start to end step n
statement/statements
Next counter
The above loop uses a counter that increases/decreases the value
during each repetition of the loop. As long as the condition is true
the statements are executed once the condition is false the
statement after next is executed.
Eg. s=0
For i=1 to 5
s=s+i
Next i
Event driven programming : Forms and controls are the basic
elements in the user interface of any windows application. In
VB these elements are called objects because they are
manipulated like objects. Objects have properties and
methods.
A few common Properties which apply to most of the objects
are name, caption, text, visible, font etc.
Common Methods are:
Objects have methods like
a)Clear- tells the control to discard its contents.
b) Additem, Removeitem : used to manipulate the items in a
listbox .