Cs1 Visual Basic
Cs1 Visual Basic
TPSComp.Sci.Visual Basic
Comp.Sci..Visual Basic 4-2
TPS
4 Object- oriented;
Another important concept which has been incorporated in
VB is the concept of object. Object provides a way to bind code
with data into a package.
5) Eventdriven
Inold Basic we had to write code for processing the tasks in a
sequential order. But in windows environment the program is
to watch for the occurance of user events such as pressing a
key or clicking the mouse. VB program is acts only to events
when an event oCcurs. VB executes the code associated with
that event.
General
shapes Lines
Object lInking
embedding
m+m -2h
SComp.Sci.VisualBasic 4-6
Q8
Explainiif..then statement with suitable example.
:IE...
Then structure sets the condition specified and if the
Ans
conditionis true executesth¹ statement that follows.
structure can have a single line or amultiple line syhtax.
Single line syntax:
statement
IF condition then
End IF
Then
e.g IF Marks< 35
Print "Fail"
End IF
Multiple line syntax:
E Condition Thern statement
If Month (date) =1 then
year=year + 1
End it
Click on file menu and then click on the "save project". First
save the form with extension ".frm" and then save the project
with extension ".vbp"
loop
OR
TPSComp.Sci.Visual Basic 4 11
Syntax : Do
Statement
loop while condition
Cg.: Do
total total + i
wend
Comp.Sci.Visual Basic
4-12
Explainthe Events of Object?
Ql4A Common event of object: Their are two common event of
Ans.
object
1)Mouseevent 2) Keyboard event
Mouse Event :Mouse event oCCured by mouse action.
Gingle Click : The Single click event takes place when the user
licks the lett mouse button.
ii) Double Click : The double click event takes place when the
user double clicks then left mouse button.
ii) Mouse down event : Mouse down event takes place when
mouse button is pressed.
iv) Mouse upevent: Mouse up event takes place when mouse
button is released.
v)Mouse Move : Mouse moveevent takes place continuosly as
the mouse is moved over a control.
) Keyboard event : Keyboard event occured by keyboard event
or keyboard action.
i) Keydown event : Keydown event is takes place when a key is
pressed.
iü) Keyup event : Key up event is takes place when a key is
releasing
Si) Key Press : Keypress event is find out which key is pressed.
This event isused frequently to write keyboard handles for
text boxes because this event takes place before the character
pressed is display in the text box.
Iv) Changeevent: Change event takes place by variouscontrols
when their is a change.
Q15 What is array? How is it declared and initialise in VB.
Aray : Array is a group of element or atoms.
Declairing array :
Dim Salary (15)
this example salary (0) is first persorn salary, salary (1) sec
ond person salary and so on
Initialising Array :
Salary (2) =10000
TPS Comp.Sci.Visual Basie
Salary (0) - 15000
You can also specify the type of element with
declaration as follows,
1) Declearing array : Dim sallary (10) As ínteger
2) Initialising array:
salary (0) - 15000
salary (2) = 10000
Dim name (10) as string
name (2) = "India"
name (0) ="Maharashtra"
Multidimention array :. Atwo dimention array has two
The first identifies the row and second indice
identify the column.
e.g. Dim a (15) (15) as integer
VISUAL BASIC PROGRAMS
1) Program in Visual Basic to find area of circle Form.
Area of circle
Text bo1 2
Area of circle
command 2
Sum &Average
Number 1 text 1
Number2 text 2
Number 3 text 3
text 4
Sum
text 5
Average
Code
Private sub comand1_click()
text4.text= val(text1. text) + val (text2.text) + val (text3.text6)
End sub
Private sub command 2_click( )
text5.text = (text4.text) /3
End sub
Private sub command3_click()
text1.text = " n
text2,text =
text3.text =
text4.text = " "
End sub
Private sub command4_click()
uhload me
End sub
TPS Comp.Sci.Visual Basic 4 - 15
3) Write a progran in visual basic to find sum of 100
entered. number
Form Form 1
Com mand 2
c=1
do while c< =100
apsaranon
èrasing
i= inputbox ("Enter 100 Numbers") USercse
Sum =Sum +1 clean
C=C+ 1
&
neat
loop for
text1.text = sum
End sub
End sub
VisualBasic
Comp.Si. 4-16
15
Write a programin Visual Basic that displays a picture with
red colour as a background on form load event and when
drag drop operation is carried out background colour must
changetO green and when drag over operation is carried out
background colour must change to blue.
Ans:Form-
Form
Picture
box
Command
button Dragthis button
Sum of 1to 10
Number
C=1
do while c< =10
Sum = Sum +c
C=C+ 1
loop
text1.text sum
End sub
Private sub command2_click()
unload me
End sub
Private sub command3_ click( )
text1.text = ""
End sub
13Comp.Sc..
Visual Basic 4-18
Form
Form
Enter Number
Display
command1_Click( )
Code: Private sub
Dim n, i, r as integer
n = textl.text
For i= 1to 10
r=nxi
"&r
Print n& "x " &i&"=
Next i
End sub
between 1 to 50.
a program in VB to
find sum of even nos
I: Write
X
Ans: Form Form
Clear Exit
Sum
TPS Comp.Sci.Visual Basic 4 -19
Code : Private sub command1_Click( )
Dim i, sum as integer
Sum = )
For i =0 to 50step 2
Sum Sum + i
Next i
text1.text = sum
End sub
Private sub command2_click( )
text1.text = "
End sub
Private sub command3_ click( )
unload me
End sub
8. Program in VBto form number divisible by 5 between 1 to
100.
Ans :
Form Form
SHOW
EXIT
Nexti
End sub
Privatesub command 2_click( )
unload me
End sub