Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

Bit 3102 Practical Notes

Uploaded by

Prince Baraka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Bit 3102 Practical Notes

Uploaded by

Prince Baraka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

VISUAL

BASIC6.0
Visual Basic 6.0
 VB is an Event- Driven programming
 VB has different objects called Forms and Controls.
 Each object has its own properties (Colour, height, name etc)

• Visual Basic programs display a Windows style screen (called a form)


with boxes into which users type and edit information and buttons that
they click to initiate actions.


• Controls: The boxes and on the form (text boxes, command button etc
) are referred to as controls.

• Form and controls are called objects.


HOW TO START VISUAL BASIC?
TO START VISUAL BASIC,THE STEPS ARE-

1.clickon start all programs


microsoft visual basic studio 6.0
microsoft visual basic 6.0
2.the new project window appears
(fig.1.1)

the new project window has three tabs:-

1)new:to start a new project.

2)existing:to select from a list of


existing projects.

3)recent:to select from the list of


recently opened projects.in

FIG.1.1 NEW PROJECT WINDOW the new tab ,select standard.exe


application .this opens the microsoft
visual basic window.
COMPONENTS OF VB WINDOW
Title
bar
Project
Menu window
bar

Properties
window
Tool
bar

Form
window

Propertie
FIG 1.2 COMPONENTS OF WINDOW s layout
Tool box
window
MENU BAR AND TOOL BAR
Menu
bar

FIG 1.4 MENU BAR AND TOOL BAR

MENU BAR-
The menu bar(fig 1.4) consists of
Many drop-down menus.Clicking on any of
The option ofthe menu bar makes it
Active.
TOOL BAR-
The tool bar(fig 1.4) has buttons that
Provide some of the menu options.
FORM WINDOW
The form window is the primary
work area that forms the
background of the application
.The user can draw objects on
the form.
 TOOLBOX
The toolbox contains icons that
represent commonly used controls
such as label, textbox, command
button ,etc.
PROJECT WINDOW
IT DISPLAYS A LIST OF ALL FORMS THAT MAKES UP
THE APPLICATION.IT HAS THREE BUTTONS ON THE
TOP LEFT.(FIG 1.7)
1.CLICK ON VIEW OBJECT TO OPEN A FORM.
2.CLICK ON VIEW CODE TO OPEN CODE WINDOW.

View Select
Code form

View
object

FIG 1.7 PROJECT WINDOW


PROPERTIES WINDOW
The properties window lists the property of the selected
Controls or for the form.

The property window has two tabs:


1.The alphabetic tab list all the properties in alphabetic order.(Fig 1.8)
2.The categorized tab lists the properties category-
wise(fig 1.9)

FIG 1.8 ALPHABETIC TAB OF PROPERTIES FIG 1.9 CATEGORIZED TAB OF


WINDOW
PROPERTIES WINDOW
How to place controls on the form?
 Click on the control icon in the toolbox and then
click on the desired location on the form.Drag the
mouse diagonally so that the control has the desired
size.(Fig 1.14)

 To reposition the control ,hold down the left mouse


button and drag the control to a new location,

 To resize the control,click the control,drag one of its


edges or corners.
Properties:
Caption : it sets the title of the form
Name :it gives the name of the form with which it is
referred to in the code. The default value is form.
Appearance: value 1 makes the form look like
three- Dimensional.Value 0 makes the form look
flat.
Font:it sets the font type ,font style and font size
for the text.
Fore color :it sets the color for the text on the form.
Enabled:value true makes the form respond to
mouse And keyboard events.Value false makes
the form responding to any event.
Back color:it sets the form’s background color.
Visible: value false makes the form invisible
LABEL
Labels are used to display heading and messages
PROPERTIES
NAME: This hold the name of the label. E.g lblName, iblCity.

CAPTION: This is the text displayed in the label.


Font: This sets font,font style and font size.
Border style:This determines the type of border.
Appearance: this is 3-d in appearance.
Events
Click: this event is triggered when the user clicks on the
label.
Double click:This event is triggered when th user double-
clicks on the label
Textbox
The text boxes are used to enter / accept the
information.
Properties
Name:This is the name used to refer to the
textbox in the code. E.g txtName, txtCity
Appearance: This is 3-d or flat in appearance
Maxlength: This limits the length of the text.
value 0 indicates limited length.
Multiline: This specifies whether the textbox
displays a single line or multiple
lines.
PASSWORD CHAR:THIS HIDES THE TEXT WITH A
SINGLE CHARACTER(USUALLY*IS
USED).
ENABLED:VALUE TRUE/FALSE DETERMINES WHETHER
THE TEXTBOX RESPONDS TO ANY EVENT OR
NOT.
LOCKED:VALUE TRUE/FALSE DETERMINES WHETHER
THE TEXTBOX RESPONDS TO ANY EVENT OR
NOT.
LOCKED:VALUE TRUE/FALSE DETERMINES WHETHER
THE TEXTBOX CONTENTS CAN BE CHANGED OR
NOT.
TEXT:THIS IS THE STRING TO BE DISPLAYED IN THE
TEXTBOX.
EVENTS
CHANGE:THIS EVENT IS TRIGGERED EVERY TIME WHEN
THE TEXTBOX DISPLAYS A SINGLE LINE OR
MULTIPLE LINE.
COMMAND BUTTON
The command button is very important control as it is used to
execute commands
PROPERTIES:
Name :Holds the name of command button.
E.g cmdSum, cmdExit
APPEARANCES: This is 3-d or flat in appearance.
Caption: This is the string that is displayed on the
command button.
Visible: value true/false determines whether it is
visible or not.
Font: This sets font, font style and font size.
Enabled: value true/false determines whether it responds
to an event or not.
Events click:this event is triggered when it is selected by
cllicking on it.
Val() Function:
It is used to convert a text into numeric value
that can be used for arithmetic calculation.
FormatNumber():
This function a string of character formatted as
specific number of digits after the decimal
point for the given value. The thousand
separator is added to the value by default.

e.g Amt=2564.5643
FormatNumber(Amt,2) will display 2,564.56
FormatNumber(Amt,0) will display 2,565
Simple project in VB
Object property Value
Form Name frmTybcom
Caption My First Form
Label Name lblName
Caption Good Morning Students
Command Button Name cmdWelcome
Caption &Welcome
Command Button Name cmdExit

Caption &Exit
• Steps to Create Project:
 Click at File New
 Go to property window change Name as
frmTybcom and Caption as My First Form
 Click at Label from the Toolbox, Go to Form and
draw a rectangle for message
 Goto property window change Name as lblName
and caption as Good Morning Students
 Click at Command button from the Toolbox, goto
form and draw rectangle for Welcome
 Goto property window change Name as
cmdWelcome and Caption as Welcome
 Click at Command button from the Toolbox, goto
form and draw rectangle for Exit
 Goto property window change Name as cmdExit and
Caption as &Exit
 Double click on Welcome command button to go to
the code window and type
 MsgBox “Welcome to learn Visual Basic” in between
Private Sub cmdWelcome_Click and End Sub
 Double click on Exit command button to go to the
code window and type End in between Private Sub
cmdExit_Click and End Sub
 Click at File Save project
 Press F5 to Run or Run Start
Practical No:1
1.Write a project in VB to design a suitable form to
add two numbers and display their sum

Enter Number1

Enter Number2

SUM IS

SUM CLEAR EXIT


Controls and their properties

Object Property Value /setting


Form Name frmSum
Caption Sum
Label1 Name lblNum1
Caption Enter Number1
Label2 Name lblNum2
Caption Enter Number2
Label3 Name lblSum
Caption Sum is
Object Property Value /setting
Text1 Name txtNum1
Text Blank
Text2 Name textNum2
Text Blank
Text3 Name txtSum
Text blank
Command1 Name cmdSum
Caption Sum
Command2 Name cmdClear
Caption Clear
Command3 Name cmdExit
Caption
Prof Anil Khadse
Exit
• CODE:
• Command1
• Private Sub cmdSum_Click()
• txtSum=Val(txtNum1) + Val(txtNum2)
• txtSum=FormatNumber(txtSum,2)
• End Sub
• Command2:
• Private Sub cmdClear_Click()
• txtNum1=“ “
• txtNum2=“ “
• txtSum=“ “
• End Sub
• Command3:
• Private Sub cmdExit_Click()
• End
• End Sub
Practical.No:1
2. Write a Project in VB to design a suitable form to enter
total sales and calculate and display the Commission @8%
Controls and their Properties:
Object Property Value /setting
Form Name frmComm
Caption Commission
Label1 Name lblSales
Caption Total Sales
Label2 Name lblComm
Caption Commission
Text1 Name txtSales
Text “ ” (Blank)
Text2 Name textComm
Text “ ” (Blank)
Object Property Value /setting
Command1 Name cmdCalculate
Caption &Calculate P

Command2 Name cmdClear


Caption &Clear
Command3 Name cmdExit
Caption &Exit
CODES FOR COMMAND:

Private Sub cmdCalculate_Click()


txtComm = Val(txtSales)*0.08
End Sub

Private Sub cmdClear_Click()


txtSales =“ ”
txtComm=“ ”
End Sub

Private Sub cmdExit_Click()


End
End Sub
PracticalNo:2
Write a Project in VB to prepare a form as follows
to enter principal amount, years , rate of interest
and calculate simple and compound interest.
Principal Amount

No. of years

Rate of Interest

Interest Amount

Simple Compound Exit


Controls and their properties
Object Property settings

Form Name frmint


Caption Interest Calculation
Label1 Name lblPamt
Caption Principal Amount
Label2 Name lblYears
Caption No. of years

Label3 Name lblRate

Caption Rate of interest

Label4 Name lblInterest

Caption Interest Amount


Controls and their properties
Object Property settings
Text1 Name txtAmt
Text “ ” (Blank)
Text2 Name textYears
Text “ ” (Blank)
Text3 Name txtRate
Text “ ” (Blank)
Text4 Name textInt
Text “ ” (Blank)
Locked True
Object Property settings
Command1 Name cmdSimple
Caption &Simple
Command2 Name cmdCompound
Caption &Compound
Command3 Name cmdExit
Caption &Exit
Controls and their properties
Codes for the different commands:
Private Sub cmdSimple_Click()
txtInt = (Val(txtAmt)*Val(txtyears)*
Val(txtRate))/100
txtInt= FormatNumber(txtInt,2)
End Sub
Private Sub cmdCompound_Click()
txtInt = Val(txtAmt)*(1+Val(txtRate)/100) ^
Val(txtYears) – Val(txtAmt)
txtInt = FormatNumber(txtInt,2)
End Sub
Private Sub cmdExit_Click()
End
End Sub
PracticalNo:
Write a Project in VB to design a suitable form to enter Basic
Salary and calculate and display the DA @85% of Basic Salary

Controls and their properties

Object Property settings

Form Name frDA


Caption DA
Label1 Name lblBasic
Caption Basic Salary
Label2 Name lblDA
Caption DA
Controls and their properties

Object Property settings


Text1 Name txtBasic
Text “ ” (Blank)
Text2 Name textDa
Text “ ” (Blank)
Command1 Name cmdCalculate
Caption &Calculate
Command2 Name cmdClear
Caption &Clear
Command3 Name cmdExit
Caption &Exit
Private Sub cmdCalculate_Click()
txtDa = Val(txtBasic)*0.85
End Sub
Private Sub cmdClear_Click()
txtBasic =“ ”
txtDa=“ ”
End Sub
Private Sub cmdExit_Click()
End
End Sub
form to allow the user to enter name of
client,Number of share purchased from
broker and rate. Calculate &display amount
to be paid to the broker.
2. Write a Project in VB to design a suitable
form to enter the length of the sides of a
square. Calculate and display its area and
perimeter
[area = side x side, Perimeter = 4 x side]

3. Write VB project to accept Name of student


and marks in three subjects, Find and display
average marks
Variables and constants
Variables: It is the value changes during the
program execution.
Declaration of variables:
Dim A As Single : floating point number
Dim B As Single with six digit of accuracy
Dim Age As Integer : Whole number

Dim Price As Currency: decimal fraction


such as Rs & Paisa
Scope of Variables:
• Local declaration- value is available for
particular procedure
• Module Level declaration
This type is required if you want variable in
more than one procedures of Form
• Global Declaration:
This type is required when you want to use
variable anywhere in the project.
Rules for Variable name and convention:

 Maximum 255 character can be used.


 Name can contains alphabets, digits &
underscore.
 It should not be reserved word.
 First character must be capital letter
Constants: The data cannot change during the program
execution is called constant.
Intrinsic constant: readymade in VB i.e vbBlue, vbRed
Message Box Statement:
It is special type of VB window in which you
display message to the user.(it can be any
message)
Format is
MsgBox “Message”, Button/Icon

MsgBox “Message” str sum,vbOkOnly


Button Value Constant Icon

OK 0 vbOKOnly No Icon
Critical Message 16 vbCritical
Icon
Warning Query 32 vbQuestion
Icon
Warning Message 48 vbExclamation
Information 64 vbInformation
Message
Decisions and conditions
If….Then….Else Statement

This statement is used to take certain decision which is


based on the condition.
Format is
If Condition Then
Statement/s
Else
Statement/s
End If
Nested If
If Condition Then
Statement/s
Else If Condition Then
Statement/s
Else
Statement/s
End If
e.g
If age>=18 Then
MsgBox “Eligible for voting ”
Else
MsgBox “Not Eligible for voting ”
End If
1. Write a project in VB to design a suitable form to
enter height of a person and display the a message
“Allowed to take a ride” if the height is greater than
55 inches otherwise “Sorry , Not Allowed to take
ride”
Controls and properties:

Object Property Value


Form Name frmRide
Caption Ride
Label Name lblHeight
Caption Height
Text Name txtHt
Caption “ “
Object Property Value

Command Name cmdDecision


Button
Caption &Decision
Command Name cmdClear
Button
Caption &Clear
Command Name cmdExit
Button
Caption &Exit
CODE:
Private Sub cmdRide_Click()
Dim H As Integer
H=Val(txtHt)
If H> 55 Then
MsgBox “Allowed to take a Ride”
Else
MsgBox “Sorry, Not Allowed to take a Ride”
End If
End Sub
Private Sub cmdClear_Click()
txtHt = “ ”
End Sub
Private Sub cmdExit_Click()
End
End Sub
2.Write a project in VB to design a suitable form
which allows the user to enter the name and basic
salary and to calculate Bonus as 30% of basic salary
whenever basic salary is less than 10000 otherwise
40% of basic salary using CALCULATE button and
display the result.
Object Property Setting
Form Name frmBonus
Caption Bonus
Label1 Name lblName
Caption Name
Label2 Name lblBasic
Caption Basic Salary
Label3 Name lblBonus
Caption Bonus Amount
Text1 Name txtName
Text Blank
Text2 Name txtBasic
Text Blank
Text3 Name txtBonus
Text Blank
Command1 Name cmdCalculate
Caption &Calculate
Command2 Name cmdExit
Caption &Exit
Command3 Name cmdClear
CODES:
Private Sub cmdCal_Click()
txtBs = Val(txtBs)
If txtBs < 10000 Then
txtBonus = txtBs * 0.3
Else
txtBonus = txtBs * 0.4
End If
txtBonus = FormatNumber(txtBonus, 2)
txtName.SetFocus
End Sub
Private Sub cmdClear_Click()
txtName = " "
txtBs = " "
txtBonus = " "
End Sub

Private Sub cmdExit_Click


End
End Sub
3. Write a project in VB to design a suitable
form to enter the name and salary earned
by employee, Calculate and display tax to
be paid as per the following schedule.
Salary Tax
< 500000 Nil
500000 – 1000000 10%
100000 and more 20%
Controls and their properties
Object Property Setting
Form Name frmTax
Caption Tax Calculation
Label1 Name lblName
Caption Name
Label2 Name lblSalary
Caption Salary
Label3 Name lblTax
Caption Tax
Text Box Name txtName
Text “ “
Cond..
Object Property Setting
Text Box Name txtSalary
Text “ ”
Text Box Name txtTax
Text “ ”
Command1 Name cmdcalculate
Caption &Calculate Tax
Command2 Name cmdClear
Caption &Clear
Command 3 Name cmdExit
Caption &Exit
Codes for Commands
Private Sub cmdCalculate_Click()
Dim S,Tax As Single
S=Val(txtSalary)
If S<500000 Then
Tax = 0
Else If S<1000000
Tax = S*0.10
Else
Tax= S*0.20
End If
txtTax=FormatNumbe(Tax,2)
txtName.SetFocus
End Sub
Private Sub cmdClear_Click()
txtName = “ ”
txtSalary = “ ”
txtTax= “ ”
End Sub
Private Sub cmdExit_Click()
End
End Sub
4. Write a project in VB to design a suitable form
which allow the user to enter 3 numbers and find the
largesst of these numbers and display the result in
message box
Controls and properties:
Object Property Setting
Form Name frmLargest
Caption Largest
Label1 Name lblNum1
Caption First Number
Label2 Name lblNum2
Caption Second Number

Label3 Name lblNum3


Caption Third Number
Label4 Name lblLargest
Caption Largest
Text Box Name txtNum1
Text “ “
Text Box Name txtNum2
Text “ “
Text Box Name txtNum3
Text “ “
Command1 Name cmdLargest
Caption &Find Largest
Command2 Name cmdExit
Caption &Exit
Codes:
Private Sub cmdLargest_Click( )
Dim A, B, C, Largest As Integer
A = Val(txtNum1)
B = Val(txtNum2)
C = Val(txtNum3)
If A > B And A > C Then
Largest = A
Else
If B > C And B > A Then
Largest = B
Else
Largest = C
End If
End If
MsgBox "Largest Number is" & Str(Largest)
End Sub
Private Sub cmdExit_Click
End
End Sub
For -------Next loop

It is used when statement or statements are to be


repeated
This loop uses variable called loop index.
The loop index determines the number of times loop
will be executed.

For E1 = E2 To E3 Step E4
----------------
Statements to be repeated
----------------
Next E1
Where E1 = Control or Index Variable
E2 = Initial value
E3 = Final value
E4 = Increment in E1 after every repetition

Example
For I=1 to 5
Sum=Sum+I
Next I
PRACT_NO-12
1. Write a project in VB to design a suitable form which
allow the user to enter any integer and then display the
sum of all integer upto that integer
( i.e 1 + 2 + 3 + 4 + ……… + n)

Object Properties Setting


Form Name frmSum
Caption Sum
Label1 Name lblInt
Caption Enter Integer
Text Box Name txtInt
Text “ “
Command 1 Name cmdSum
Object Properties Setting
Command Button 2 Name cmdClear
Caption &Clear
Command Button 3 Name cmdExit
Caption &Exit
Codes:
Private Sub cmdSum_Click( )
Dim I As Integer
Dim N As Integer
Dim Sum As Integer
N = Val(txtInt)
Sum = 0
For I = 1 To N
Sum = Sum + I
MsgBox”Sum of Integer is” &Sum,vbOkOnly
End Sub

Private Sub Clear_Click()


txtInt= “ ”
End Sub

Private Sub Exit_Click()


End
End Sub
2. Write a project in VB to design a suitable form which
allow the user to enter any integer and then display the
sum of squares of integer upto that integer.
3. Write a VB project to compute and display the sum
of series 2 + 5 + 8 + 11 + ………….+ 32

Object Properties Setting


Form Name frmSum
Caption Sum
Command 1 Name cmdSum
Caption &Sum
Codes:
Private Sub cmdSum_Click( )
Dim I As Integer
Dim Sum As Integer
Sum = 0
For I = 2 To 32 Step 3
Sum = Sum + I
Next I
MsgBox”Sum of Integer is” &Sum,vbOkOnly
End Sub
3. Write a VB project to compute and display the
factorial of n ( 1*2*3*4……….n)
Object Properties Setting
Form Name frmFactorial
Caption Factorial
Lable 1 Name lblNumber
Caption Number
Text Box1 Name txtNum
Caption
Command 1 Name cmdFact
Caption &Factorial
Codes:
Private Sub cmdFact_Click( )
Dim I As Integer
Dim N As Integer
Dim F As Integer
N=Val(txtNum)
F=1
For I =1 To N
F = F*I
Next I
MsgBox “Factorial is ” &F,vbOkOnly
End Sub
THANK YOU

You might also like