You are on page 1of 20

VISUAL BASIC

Q.1 What is Visual Basic?


Ans : Visual Basic is a tool that allows you to develop window,
User Interface) GUIapplications.
(Graphic
The application are
1) Visual Design
2) Event driven Programmingmeaning code remains idle .n
Visual Basic is event, driven,
respond to some event (button pressing, menu
called upon to
selection etc)
event prccessor.
Visual Basic is governed by an detected. Once an event is
an event is
Nothing happens until
detected the code
corresponding to that event (event
executed. Program control is then returned to the
procedure)is
event processOr.
features of VB?
Q.2 What are the special
Ans:Special features of VB
Environnment:
1) Integrated Development
developers to create the user interface for his
IDE helps the simply by selecting the
application before writing any code,
dragging the mouse.
controls from the toolbox and then
2) Built in code In Cor
at built in code. code
VB has also provided a wide varietywrite each and every
to
Ctt the programmer himself has
3) Controls : controls
number of
Every Visual Basic application consists a useand
bus}
ready to
These controls provide alarge number of
free code. 4-1

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.

0.3 Explain the following terms used in VB.


Ans : 1) Controls:- Controls are the tools available on the Toolbox
window, such as push buttons, radio buttons, check boxes
etc. You can place them on a form to interact with the user and
to control the program flow.
2) Form: It is the main window. It binds the display of the
window with its controls, the code generated by them
whenever controls are used, they mnust be placed in a form.
3) Application :- Anapplication is acollection at one or more
files that compile into a single executable program.
4) Project Aproject is a collection of files.
5) Wizards:-Wizards are question and answer dialogue boxes
that automate tasks.
6)Code:- Code is another name for the program. It binds all the
graphics, text and processes together within an application.
Q.4 State use of following controls in VB.
Ans :1) Text box :- This control displays text that the user can edit.
"Text"properly can set the text that the user enters.
2) Lable :-This control displays text on a form that the user can.t
edit.
3) Erame :- used to draw boxes on the form and to group other
elements.
TPS Comp.Sci. Visual Basic
4) picture box :- It is used to display images
5) Checkbox :- The checkbox control presents one one or more
User can select select one or more at them. The choice
main
The prvaloperuety
either 0 or 1.
at this control is value which is
indicates thatthe check box is checked and the value 0
that the checkbox cleared.
is used for turning on indicate
It is
partiular feature of the program .
Joff. Sorme
6) Option button :- They appear in groupS and user can cL
only one of them at a time. The main properly of option Buttor
checked and false
is checked. It is True if the control is
Combo box :- Similar to the list box control
but it otherwise.
7) containg
text edit field, The user can enter/ choose an itemn from the lis
field.
and enter anew string in the edit
8) List box:- It contains a list of options rom which the user ca
seBect one or more.
ltem.
The user can sCroll the list tolocate an
Horizontal and Vertical scrollbar :- It let's the user specify a
9)
magnitude by scrolling the controls button between it
minimum and maximum value.
10) Timer:- To perform tasks at regular intervals. Main property i
notifies yours
" Interval" which determines how often the timer
application.
rectangle
11) Shape :- Used to draw graphical elements. such as
circles or elipses on a Form.
12) Line :- It is used to draw lines on a form.
the picture box control. It
13) 1 Image :- This control is similar to
used to display a picture.
click access
14) Data Control:-This control provides point and
.data stored in databases.
yourfoE
OLE:- This control is a window you carn place on MSworl
15) as
to host documents from other applications such
or Excel.
SComp.Sci.Visual Basic 4- 4

General

Pointer Picture box


lable A
Text box
Frame Command button

Check bOX Option button


Combo box Llist box

HorizontalScroll Vertical scroll bar


bar
Timer
Drive list box

Directory list box File list box

shapes Lines

Image bOX Data tool box

Object lInking
embedding

Fig Layout of toolbox

Q5 What are data types available in visual Basic? Explain it.


Ans: Visual Basic has six data types
1)Numeric 2) string 3) Boolean
4) Date 5) object 6) variant
INumeric :- There are 5 types of numeric data
a) Integer b) long ) single
d) Double e) currency
String:- VB defines string as adata types. There is practically
no restriction on the length at a string. Null string is at length
3) Boolean:- This data type stores true (1), False (0) values.
TPSComp.Sci. Visual Basic
) Date;- This data type holds the calender date and
time.
5) object: An object variable referes to an object ar V
VB
a command button or a checkbox. It enables the Sudh
change the properties at an object.
6) Variant:- Avariable at data type variant can hold
prOgram
of data. As you as ign a value to it, it's data type willany type
Explain implicit declaration and explicit hange
Q.6
variable in visual basic? declaration
Ans: Avariable may be declared anywhere in the program or it
not be declared at all. Declaring a variable is optional, may
Explicit Declarations :- To declare a variable use the
statement, followed by the variable name Dim
and type as follows
Dim number as Intger
Dim name as string
Impliit Decdarations:- It is not compulsory to declare a variab.
VB. When visual Basic meets an undeclared variable name
creates anew variable on the spot.
Dim vall, val2
vall = "welcome"
val2 = 100
The vall variable is string variable and
val2 is a numeric variable.
Q.7 What is an array ? How is it declared in visual Basic?
Ans : An array is a set of values represented by a single name. Th
numbers of values in the array is called the size of the array. Eac
individual value is identified by its position in the array. This number
iswritten in parenthese after the array name and it is called the inde
The size of the array is stated when the array is declared.
ForExample- Dim Marks (10) As Integer is the declarationvalue
th
Marks is the name of an array having 11 values. The individual
are Marks (0), Marks(1), Marks(2), ..., Marks(9) and Marks(10). Th
index should not exceed the limit.
willh
For Example- With the above declaration, Marks(12),
an error.

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

Q.9 Explain the following window. in the


Ans :1) Form layout window : The form layout window is
lower-right corrner ofthe VB IDE. It displays the initialposition
of the forms. This window is useful in the applications that
use multiple forms because it can be specified position of each
form w.r.t. main form.
2) Project Explorer : This window displays the componenets of
the project. Simple projects are made of a single form. This is
called project explorer because it has look of window Explorer.
3) The properties window : This window contains the property
settings for the selected control. Properties are the attributes of
an object such as size, caption, color etc. By selecting control
We can edit the property of a contract from properties window.
4
The form designer window :
It 1s the main window in the middle of the screen. In this
Window we design and edit the applications user interface. The
ae window displays text editer window in which we can
CIlter and edit the application code. Theses two window are
called a) form window b) code window.
TPSComp.Si.Visual Basic
5) Toolbox: This window contains the icons of the control
can be placed on aform to create the applications User which
inteface
By default, the toolbox contains the pointer icon and the
of 20 Active x controls. To place the control on the icony
click &drag control onto the form by doubleclickingform,
the
we
6) The immediate window :
icon,
A the be tom of the IDE is a debugging aid. We can step int.
the applications code while it excutes, change the values of the
variable or evern insert statements in the code, and then
the execution of an application.
continue
Q. 10Select the correct alternative and rewrite the following.
1) is the extension for Visual Basic Project.
a) vbp b) exe c) vbs d) vba
Ans : a) Vbp
2) control appearsqin group and user can choose only
one of them.
a) Check box b) option button
c) command d) None of these
Ans : b) option button
control displays list of all folder on current driver.
3)
a) Drive list box b) Directory list box
c) File list d) None of these.
Ans : b) Directory list box
control is used to insert document from other
4)
application.
a) OLE b) shape
c) OEL d) None of these
Ans : a) OLE
5) function displays current date and time.
a) now ) b) date )
3) day () d) None of these
Ans : a) now 0
6) type of variables sotres either true or false value.
d) variant
a) Boolean b) Uject c) integer
Ans : a) Boolean
Basic 4-8
COTdis used for aining the controls
b) Fonat O Run d) View
Format
.
statemet is used to tminate for 100p.
2 e b) Nat c) Do d) Go

For Flat ook in VB the appearence should be


a)0 b) 1 c) None of these d) 10

O11 What are commands in Meru Bar of visual Basic? Explain?


ATs :Visual basic meu bar contains folowing commands
Fle - It contains commands for opening and saving projects
and crating exeutable files ard a list of recent objects.
Edit - It contains editing commands suh as copy, paste, find
nd replace.
View - It contains commands for showing or hiding
components of the IDE
Project - It contains commands that add components to current
project, references to windows objects and new tools to the
tool box.
Format - It contains commands for aligning the controls on
the form.
Debug - It contains debugging commands.
Run - Itcontains commands that start, break and end execution
of current application.
Query -It contains buiding of database application.
Diagram -It contains commands for editing database diagrams.
0. Tools - It contains tools for building Active x components and
controls.
1. Add-Ins - It contains add-ins that you can add and remove as
needed.
2 Window - It contains commandsto arrange windows on screen.
3. Help - It contains information to help for your work.
TPS Comp.Sci. Visual Basic 4-9
Q.12 Explain the structure of Visual Basic program with ond
suitable example.
Ans. There are five steps of structure of V.B. Programming.
1) Designing a form.
2) Setting the property of selected control.
3) Coding.
4) Naming &saving the project.
5) Run the apphcation.
the
e-g. To display the "Welcome to VB" in the text box when click
command button "OK".
Step 1 - Designing a Form.
Form 1
Text 1
Command 1

command button in the


In this step, select the one text box one
tool box.
Step 2 - Setting the properties of selected control.
1) Select the command button 1and then select the caption prop
write the "oK"
erty in property window and
2) Selected the Text box 1 and find the 'texe property in property
text box.
window and press the backspace. i.e. clear the
Form 1
-Text box
-Command button
OK

& one command button in


In thisstep, select the one text box
the tool box.
Step 3
button "OK" and write
Coding :A double click on command
the code as following
Private sub command button1 -click ()
Text 1. text = "welcome ta VB" End sub.
Step 4
Naming &saving the project
SComp.Sai.
yVisual Basic 4-10

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"

Run the application.


Press F5 Or click the start in the Run Menu.
Q13 What is Dynamic Array? Explain with example.
fyou insert alarge amount of data then declare a dynamic
Ans. 1)Ify
array.
2) The size of dynamic array is variable during the execution of
program.
3 To create a dynamic array declare it with 'dim' statement but
not specify ie's dimension.
e.g Dim a () as integer.
4 Later in the program, when you know how many element
you want to store in the array the nuse the Redim' statement.
e.g. Dim a () as integer.
redim a(15) as integer.
Q13(a) Explain the loop statements in VB.
Ans. Loop statement :
There are three types cf loop statement.
1) Do loop 2) For - Next 3) While wend
1) Doloop: There are two variation of the 'do loop' statement.
1) Do - while loop. 2) Do - until loop
2) Do -while loop: Aloop can be executed while the condition
is true
syntax :
Do while condition
Statement
Loop
eg: Do while i <11
total = total + i
0+

loop
OR
TPSComp.Sci.Visual Basic 4 11
Syntax : Do
Statement
loop while condition
Cg.: Do
total total + i

loop while i< 11


2) Do - until loop: Aloop can be executed until the
is true condition
snta
Do until condition
statement
loop
eg: Do untill i<ll
total = total +i
i++
loop until i <l
2) For - Next: The for next require that how many times the
statements in the loop will be executed. For Next loop uses a
counter that increases and decreases. in value during each
xepetition of the loop
Syntax:
For counter start to End (Step incremnent)
Next (Counter)
eg.:
For i = 1to 10
step 2
Next i
3) While wend:The while wend loop execute a block of statements
while a condition is true
Syntax :
While condition
Statement
wend
eg:While i <l|
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

Enter Radlue Test bro 1

Text bo1 2
Area of circle

command 1 Area Cloar Exit command3

command 2

Code - Private sub command1_click( )


text2.text = 3.14 * text1.text * text1. text
End sub
Private sub command 2_cick ()
text1. text = M n
text2. text = 4 n
End sub
Private sub command3_ click( )
unload me
End sub
PSComp.Sci..VisualBasic 4-14
Programin Visual Basic to find sum and average of three
numbers.
Form

Sum &Average

Number 1 text 1

Number2 text 2

Number 3 text 3

text 4
Sum

text 5
Average

AVERAGE Clear Exit Com mand 4


Sum

command 1 Com mand 2 Command 3

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

Label Enter 100 Numbers Text 1

command 1 Start Exit Clear -com mand 3

Com mand 2

Code: Private sub command1_Click( )


Dim sum, i cas integer
Sum =0

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

Private sub command2_click( )


unload me
End sub

Private sub command3_ click()


textl.text'= "

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

Code -select dragdrop and dragover property of picture box


private sub picturel_dragdrop (source as control, Xas Single,
Yas single, state as integer)
Picture1.backcolor - vbgreen
End sub.

Private sub picturel_dragover (source as control, Xas Single,


Yas single, state as integer)
If state = 0 then picture1.backcolor=vbblue
Itstate =1 then picture1.backcolor=vbred
End sub
In above program before writing code, set the picture box
control background prroperty to agreen colour and set the
Command button Drag mode property to 1-Automatic.
TPS Comp.Sci.Visual Basic 4 -17
5. Write a program in Visual Basic to find sum of first 10
Ans : numbT.
Form Form

Sum of 1to 10
Number

Start Exit Clear

Code: Private sub command1_Click()


Dim i, sum, c as integer
Sum = 0

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

in VB to form multiplication table.


Write a program
Ans.

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

Sum of even no's


(Range 1 to 50)

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

Code: Private sub


Dim i, as integer
command1_Click()
Print &"Nos divisible by
5(Range 1 to 100 &
sComp.si.Visual Basic
4- 20
step5
For i= 0to 100
Printi

Nexti

End sub
Privatesub command 2_click( )
unload me
End sub

You might also like