0 ratings 0% found this document useful (0 votes) 31 views 18 pages Visual Basic
The document outlines a series of practical projects in Visual Basic (VB) programming, including creating forms for calculating sums, bonuses, and allowances. Each project includes design specifications, control properties, and corresponding code snippets for functionality. The document emphasizes the use of various controls such as text boxes, command buttons, and labels to achieve specific calculations and outputs.
AI-enhanced title and description
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Go to previous items Go to next items
Save VisualBasic For Later YY
PRACTIc, 211
pevelop 4 project in yp aug
P to desi j
heir sum. “Sign a sui
play table form to add two numbers and
pistion :
yep?
esign the form as shown in Fig. P. 8.1 ber
BERS below
Fig. P. 8.1 : Sum of two variables
Controls and their properties :
Object Property Value
Form Name | frmSum
Caption ‘Sum of Two Variables
Label Name IbINum1
Caption Numi
Label Name IbINum2
: Caption Num2
| Label Name TbiSum
Caption Sum
Command Button Name cmdClear
. Caption &Clear
Command Button Name omdSum
Caption &Sum
[Command Button Name emdQuit
ae Caption &Quit
Text Box Name txtNum
Text ks
Text Box” Name eho
Text
Text Box Z Name ce :
Text
"YY. com Com, Sys. & Appl. (Sem.— VI).d Applications ~ T.¥.B.Com-(Semm,
212 9° Computer Systems anc ,
2 22 "m. Vp
Step 2 : Type the following code in the code window
Code:
"Purpose: To add two numbers
‘Author: Mukesh N Tekwani
Private Sub cmdCalc_Click()
Dim a As Integer
Dim b As Integer
Dim s As Integer
a = txtNum1.Text
b= txtNum2.Text
s=atb
txtSum.Text = s
End Sub
Private Sub cmdClear_Click()
txtNum1.Text =
txtNum2.Text ="
txtSum.Text ="
txtNum1.SetFocus
End Sub
Private Sub emdQuit_Click()
End
End Sub
Step 3: Run the program and verify the outputpevelop a project in VB to
od display the bonus which ne Sig
ution :
es Nas
Uitable
10% of sales, © °7™ to enter sales and calculat
‘ fe
Fig. P.9.1 : Bonus Calculation
Controls and their properties :
Oblect Property Value
OEE Name frmBonus
Caption Bonus Calculation
Label Name IblSales
Caption Sales
Label : Name IbiBonus
Caption Bonus
Command Button Name emdCalculate
Caption &Calculate
Command Button Name cmdClear
Caption Célear
Command Button Name emdQuit
Caption &Quit
Text Box Name txtSales
Value i
Text Box Name tatBonus
Value |ae) a7
Step 2 : Type the following code in the code windo’
Code:
‘Author: Mukesh N Tekwani
‘Purpose: Bonus Calculation
| Private Sub emdCailc Click()
Dim s As Integer ‘sales
Dim b As Double ‘bonus
S = txtSales.Text
b=0.1*s ‘bonus = 10% of sales
txtBonus.Text = b
End Sub
Private Sub emdClear_Click()
txtSales.Text =""
txtBonus.Text = ""
txtSales.SetFocus
End Sub
Private Sub cmdQuit_Click()
End
End Sub
Step 3 : Run the program and verify the output
Computer Systems and Applications = T.Y.B.Com. (Sem, ,
——tin VB to
y the DA whieh, eons
88 suity r
ok able forn
of salary, n to enter salary and calculate
pesign the fOIM a8 shown in Fy
8. P. 10.1 below,
Fig. P 10.1 - DA Calculation
Controls and their properties :
L Object Property Wane
Form Name fmDA
Caption Salary Calculations
Label Name IbIBasic
Caption Basic Salary
Label Name IbIDA
Caption DA
Text Box Name DRE
! Value ”
Text Box Name txtDA
Value ”
Command Button Name emacaie
Caption &Calculate
| Command Button Name emdClear
Caption C&lear
Command Button Name emdQuit
Caption &Quit
Step 2 : Type the following code in the code windowComputer Systems and Applications TTI Com, tig
216 22
‘Purpose: DA Calculation
‘Author: Mukesh N Tekwani
Private Sub cmdCalc Click()
Dim b As Integer ‘basic salary
Dim da As Double 'DA
b = txtBasic.Text
da=0.9*b ‘since DA is 90% of basic salary
txtDA.Text = da
End Sub
Private Sub emdClear Click()
txtBasic.Text = ""
txtDA.Text ="
txtBasic.SetFocus
End Sub
Private Sub emdQuit_Click()
End
End Sub
Se ee CEE ea nn lads ai
Step 3: Run the Program and verify the outputi 22's" 223
a PRACTICAL 13
ef determmuen the tuts valuc of anajesinodt Te given
= Siva number of yeats, This| method ‘gives! the compound
re value is the sum of principal amount and the accumulated
Tavestment amount * (1 + Interest rate) years
Xt boxes for the amount of investment, the interest rate (as a
and the number of years the investment will be held Display
value in a formatted text box as currency. Include command buttons
e, Clear, Print and Exit. Format all dollar amounts.
Amount Rate Years Future Value
|__ 2000 5
15% | 4,022.71
1234.56 7.5% | 3 1533.69
Solution :
Step 1 : Design the form as shown in Fig. P. 13-1 below :
= txtPrn
eae txtRate
ee txtYears
IbIFV sat
IbiCale cmdClear
IbiPrint cmdExit
Fig. P. 13.1
Step 2 : Type the following code in the code window
Code :
Purpose: Calculate future value of an investment
Purpose: 5
‘Author: Mukesh N Tekwani
Private Sub cmdCale_Click()
‘calculate the future value of an investment
* >
Dim'P As Double
Dim RAs Double .
Dim T As Double204 ws's° Computer Systems and Applications ~ T-Y.B.Com(Sem, yp
P = Val(txtPrn. Text)
R = Val(txtRate.Text)
T = Val(txtYears. Text)
txtFV.Text = FormatCurrency(P * (1 + R) * T, 2)
End Sub
Private Sub emdClear_Click()
‘clear the text boxes and set focus back to the first text box
txtPrn.Text = ""
txtRate.Text ="
txtYears.Text ="
txtFV.Text = ""
txtPrn.SetFocus
End Sub
Private Sub emdPrint_Click()
PrintForm
End Sub
Private Sub cmdExit_Click()
End
End Sub
Step 3 : Run the program and verify that the output tallies with that given in the
table above.
Note, while entering the interest rate, type the rate in decimal fraction. E.g,,
if the interest rate is 15%, type 0.15.Solution :
Step 1:1
PCa
ext Enter Text
txtText
fraColor
optRed
optGreen
optBlue
Controls and their properties :
Italic and Unde
t the application
ne, to form:
n and Blue color to change
1 as shown in Fig. P. 15.1 below :
at the text in
fraStyle
chkBold
chkltalic
chkUnderline
cmdExit
Object Property Taias
Horm Name frmTextFormat
Caption Text Formatting
Label Name IbiText
= eoetion Enter Text
= * Name txtText
: News fraColor
Caption Font
Option Button Nan ‘ont Color
< optRed
i Caption
Option Button Nana &Red
Capti optGreen
=n &Green
he color of%
hey,
le
of
i Object “ ote
| Option Button — ae
One optBhue
[Frame
es Caption
| CheckBox [Name
| om Caption =
| CheckBox [ Name =
| Caption | italic
MGRSCuE SR yea Nene chkUnderline
Caption &Underline
| Command Button Name chad eat
| Caption Exit -
Step 2 : Type the following code in the code window :
Purpose: Study the use of check box and option buttons
‘Author: Mukesh N Tekwani
Private Sub chkBold_Click()
|) Greolulcnecmeoeis enabled, text is displayed in Bold
‘otherwise it is displayed in Normal font
If chkBold.Value = 1 Then
txtText.FontBold = True
Else
txtText.FontBold = False
End If
End Sub
Private Sub chkltalic_Click()
‘if Italic check box is enabled, text is displayed in
Italic otherwise it is displayed in Normal font
If chkltalic. Value = 1 Then
txtText.Fontltalic = True
Else
txtText.Fontltalic = False
End If.
End Sub
Private Sub chkUnderline_Click() He
‘if Underline check box is enabled, text is underlined,
i u
‘otherwise it is displayed in Normal fon
If chkUnderline. Value = 1 Then
txtText.FontUnderline = Truese ass Computer Systems and Applications ~ T-¥.B.Com(Sem. yp
2
Else
txtText.FontUnderline = False
End If
End Sub
Private Sub cmdExit Click()
End
End Sub
Private Sub optBlue_Click()
‘If Blue option button is selected, display text in Blue
‘color. vbBlue is an intrinsic constant in Visual Basic
txtText.ForeColor = vbBlue
End Sub
Private Sub optGreen_Click()
‘If Green option button is selected, display text in Green
‘color. vbGreen is an intrinsic constant in Visual Basic
txtText.ForeColor = vbGreen
End Sub
Private Sub optRed_Click()
If Red option button is selected, display text in Red
‘color. vbRed is an intrinsic constant in Visual Basic
txtText.ForeColor = vbRed
End Sub
Step 3 : Run the program, type some text in the text box, and verify that the font
style and colors change the text.ee a ee
| Practicals "2's"
2
| PRACTICAL 16
i |
IWIMsg.
IblAmounent
fraMode
chkRail extAmtRail
chkFlight eetAmtélighe
chkcar etamtCar
IbiTotalAmount
emdClear
cmdCale ema Bxit
Code:
‘Purpose: Travelling Allowance (TA) Bill preparation
‘Author : Mukesh N. Tekwani
|Private Sub chkCar_Click()
'if the chkCar check box is selected, di
splay the
‘corresponding text box txtCar for acc,
epting distance
If chkCar.Value = 1 Then
txtCar. Visible = True
txtCar,SetFocus
Else232 o'a'a" Computer Systems and Appl
txtCar. Visible = False
End If
End Sub
Private Sub chkFlight_Click()
‘if the chkFlight check box is selected, display the
‘corresponding text box txtFlight for accepting distance
If chkFlight,Value = 1 Then
txtPlight. Visible = True
txtPlight. SetFocus
Blse
txtFlight. Visible = False
txtAmtFlight, Text = 0
End If
End Sub
Private Sub chkRail_Click()
‘if the chikRail check box is selected, display the
‘corresponding text box txtRail for accepting distance
If chkRail.Value = 1 Then
txtRail. Visible = True
txtRail.SetFocus
Else
txtRail. Visible = False
txtAmtCar.Text = 0
End If
End Sub
Private Sub cmdCalc_Click()
‘calculate total amount for each mode of transport
‘first get the distances from the text boxes
Dim dr, df, de As Double ‘dist travelled by rail, flight, car
Dim amtr, amtf, amtc As Double ‘amount for each mode
Dim gt As Double ‘grand total amount
dr = Val(txtRail. Text)
df = Val(txtFlight. Text)
de = Val(txtCar.Text)
‘now calculate and display the amount for each mode
amtr = 3 * dr
amt * df
amte = 8 * de
‘display these amounts in the text boxes
txtAmtRail.Text = amtr
ications ~ T-¥.B.Com.-(Sem, ypPracticals 2's's"
txtAmtFlight.Text = amtf
txtAmtCar.Text = amtc
233
‘calculate and display the grand total
gt = amtr + amtf + amtc
IbITotalAmt.Caption = IbITotalAmt.Caption & "&" & gt
End Sub |
Private Sub cmdClear Click()
‘clear all objects by calling the sub-procedure
ClearAll
End Sub
Private Sub Form_Load()
IbIMsg.Caption = "Click on the desired check box, and _ enter the
corresponding distance"
‘hide the distance text boxes at form load
txtRail.Visible = False
txtFlight. Visible = False
txtCar. Visible = False
txtAmtRail.Text = 0
txtAmtFlight.Text = 0
txtAmtCar.Text = 0
End Sub
Private Sub ClearAll()
chkRail. Value = 0
chkFlight.Value = 0
chkCar.Value = 0
txtRail.Text = ""
txtFlight.Text =
txtCar.Text = ""
txtAmtRail.Text = ""
txtAmtFlight.Text = ""
txtAmtCar.Text = ""
IbITotalAmt.Caption = "Grand Total"
End Sub
Private Sub cmdExit_Click()
End
End Sub
Step 3:
distenees and modes of transport.
Run the program and test for different combinations and values of
cuyy
practical ,
re ps
atl
oo
Write @ program to accept a number in a toxt 1
ee Pox and print its table in the
1x4"4
2% 4 = 8, and so on
solution :
stop 3 : Design the form as shown in Fig. P. 19.1 below :
DO Mn ero An
txtNum
IbITable
emdTable emdQuit
emdClear
Fig. P. 19.1
Step 2 : Type the following code in the code window
Code :
‘Purpose: Display table
‘Author: Mukesh N Tekwani
Private Sub cmdClear, Click()
txtNum.Text * a
IbITable.Caption =
txtNum.SetFocus
End Sub242 22's"
Private Sub cmdQuit_Click()
End
End Sub
Private Sub cmdTable_Click()
Dim N As Integer
Dim P As Integer
Dim I As Integer ‘required by the For...Next loop
N = Val(txtNum.Text)
If N >= 1 Then
IblHdg.Caption =
‘Table of " + txtNum.Text
For 1To 10
P=N*I
Str(N) + "= "+ Str(P) + vbCrLf +""
Next I
Else
MsgBox "Please enter a number between 1 and 100°
txtNum.Text ="
IbIHdg.Caption = ""
txtNum. SetFocus
End If
End Sub
Step 3 : Run the program and verify the output
Computer Systems and Applications - T-Y.B.Com.(Sem. yp
IbITable.Caption = IblTable.Caption + Str(I) + "x" +uot 29's" 243
: PRACTICAL 20
Design a form with two option buttons (Factorial and Sum of Squares). One
text box will be used to enter a mamber, Write two functions ~ one for calculating
the factorial of a'number and the other to calonlate the sumer equsmee nemiece
fo We Peleeet SAiered. Ge Glick Gf a cadsand button, call the appropriate
function and display the result in a label
Solution ;
Step 1 : Design the form as shown in Fig, P, 20.1 below :
Sc
btNum
fraCale
optFact
oprsumsq
IbIRes
cmdCale Sau
emdClear
Nig. P. 20.1
Step 2 : Type the following code in the code window
Code:
‘Purpose: Display factorial or Sum of Squares — use of option button
‘Author: Mukesh N Tekwani
Private Sub cmdCalc_Click()
Dim N As Integer
N = Val(txtNum.Text)
Value = True Then :
3 ped Caption = "Factorial = * + Str(factorial(N))
End If
TMEY.B.Com Com. Sys. & Appl. (Sem. VD244 ons Computer Systems and Applications ~ T.Y.B.Com.-(Sem. vp
If optSumSq. Value = True Then
IblRes.Caption = "Sum of squares = " + Str(sumsqrs(N))
End If
End Sub
Private Sub cmdClear_Click()
txtNum.Text = ""
optFact.Value = False
optSumSq.Value = False
IbIRes.Caption = ""
txtNum.SetFocus
End Sub
Private Sub cmdQuit_Click()
End
End Sub
Private Function factorial(N As Integer)
Dim I As Integer
factorial = 1
For I= 1ToN
factorial = factorial * 1
Next I
End Function
Private Function sumsqrs(N As Integer)
Dim I As Integer
sumsqrs = 0
Forl=1ToN
sumsqrs = sumsgqrs + I * 1
Next I
End Function
Step 3: Run the program and verify the output
Note : What is the largest number for which this code will give the correct
factorial?
a2