0% found this document useful (0 votes)
44 views

Exercise 1: Controls Name Caption

The document contains 6 exercises describing visual basic forms and controls with code examples. Each exercise contains details of form controls like labels, text boxes, command buttons, list boxes, frames etc. along with code snippets to handle events like button clicks and form loads. The code examples demonstrate basic form and control functionality like displaying messages, performing calculations, clearing fields, transferring list items etc.

Uploaded by

Claire Perez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Exercise 1: Controls Name Caption

The document contains 6 exercises describing visual basic forms and controls with code examples. Each exercise contains details of form controls like labels, text boxes, command buttons, list boxes, frames etc. along with code snippets to handle events like button clicks and form loads. The code examples demonstrate basic form and control functionality like displaying messages, performing calculations, clearing fields, transferring list items etc.

Uploaded by

Claire Perez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

EXERCISE 1

CONTROLS NAME CAPTION


Form frmWelcome Welcome to Visual Basic
Label ! lblHeader Enter your name first
Text Box txtInput
Label 2 lblOutput
Command Button cmdEnter Enter

Private Sub cmdEnter_Click()


lblOutput.Caption = "Welcome " + txtinput.Text + " to Visual Basic Programming "
lblOutput.Visible = True
End Sub
EXERCISE 2

CONTROLS NAME CAPTION


Form frmPizzaParlor My Pizza Parlor
Frame 1 fraPizza Pizza
Frame 2 fraIngredients Ingredients
Label lblPrice Price:
Text Box txtPrice

Private Sub Form_Load() Private Sub optDeluxe_Click()


optDeluxe.Value = 0 optDeluxe.Value = 1
optSpecial.Value = 0 optSpecial.Value = 0
optPrimo.Value = 0 optPrimo.Value = 0
End Sub
chkCheese.Value = 1
chkPepper.Value = 0
chkHamBacon.Value = 1
chkMushroom.Value = 0
chkOnionChili.Value = 1
chkTomatoPineapple.Value = 0
chkSalami.Value = 0
txtPrice.Text = "Php185.00"
End Sub
Private Sub optPrimo_Click() Private Sub optSpecial_Click()
optDeluxe.Value = 0 optDeluxe.Value = 0
optSpecial.Value = 0 optSpecial.Value = 1
optPrimo.Value = 1 optPrimo.Value = 0

chkCheese.Value = 1 chkCheese.Value = 1
chkPepper.Value = 1 chkPepper.Value = 1
chkHamBacon.Value = 1 chkHamBacon.Value = 1
chkMushroom.Value = 1 chkMushroom.Value = 1
chkOnionChili.Value = 1 chkOnionChili.Value = 1
chkTomatoPineapple.Value = 1 chkTomatoPineapple.Value = 0
chkSalami.Value = 1 chkSalami.Value = 0
txtPrice.Text = "Php290.00" txtPrice.Text = "Php250.00"
End Sub End Sub

EXERCISE 3

CONTROLS NAME CAPTION


Form frmListBox List Box
List Box 1 lstList1
List Box 2 lstList2
Command Button 1 cmdTransfer Transfer
Command Button 2 cmdClear Clear
Command Button 3 cmdExit Exit
Private Sub cmdClear_Click() Private Sub cmdExit_Click()
lstList2.Clear Unload Me
cmdClear.Enabled = False
End Sub End Sub
Private Sub cmdTransfer_Click() Private Sub Form_Load()
For i = 0 To (lstList1.ListCount - 1) lstList1.AddItem "January"
lstList1.AddItem "February"
lstList1.AddItem "March"
If lstList1.Selected(i) = True Then lstList1.AddItem "April"
lstList2.AddItem lstList1.List(i) lstList1.AddItem "May"
End If lstList1.AddItem "June"
Next lstList1.AddItem "July"
cmdClear.Enabled = True lstList1.AddItem "August"
lstList1.AddItem "September"
lstList1.AddItem "October"
End Sub
lstList1.AddItem "November"
lstList1.AddItem "December"
lstList1.Selected(0) = True
lstList1.Selected(1) = True
lstList1.Selected(2) = True
End Sub

EXERCISE 4

CONTROLS NAME CAPTION


Form frmListBox List Box
List Box 1 lstList1
List Box 2 lstList2
Command Button 1 cmdTransfer Transfer
Command Button 2 cmdClear Clear
Command Button 3 cmdExit Exit
Private Sub cmdClear_Click() Private Sub cmdExit_Click()
txtRadius.Text = "" End
txtArea.Text = "" End Sub
shpCircle.FillColor = &HC0C000
txtRadius.SetFocus
End Sub
Private Sub cmdGo_Click() Input", vbOKOnly)
Dim sglRadius, sglArea As Single shpCircle.FillColor = &HC0C000
constPi = 3.1416 txtArea.Text = ""
txtRadius.SetFocus
sglRadius = Val(txtRadius.Text) End If
sglArea = constPi * sglRadius ^ 2 End Sub
txtArea.Text = Str(sglArea)
shpCircle.FillColor = &HFF&

If txtRadius.Text = "" Then


txtArea.Text = MsgBox("Enter A Numerical

EXERCISE 5

CONTROLS NAME CAPTION


Form frmAssessmentSystem Tuition Fee Assessment
System
Frame fraModePayment Mode of Payment
Label 1 lblEnterTuition Enter Tuition Fee:
Text Box 1 txtEnterTuition
Command Button 1 cmdCompute Compute
Label 2 lblTotalTuition Your Total Tuition Fee:
Text Box 2 txtTotalTuition
Command Button 2 cmdExit Exit

Private Sub optCash_Click() Private Sub optTwoPayments_Click()


optCash.Value = True optCash.Value = False
optTwoPayments.Value = False optTwoPayments.Value = True
optThreePayments.Value = False optThreePayments.Value = False
End Sub End Sub
Private Sub optThreePayments_Click() Private Sub Form_Load()
optCash.Value = False Dim sglTuition, sglTotal, sglDiscount,
optTwoPayments.Value = False sglInterest As Single
optThreePayments.Value = True sglTuition = Val(txtEnterTuition.Text)
optCash.Value = 0
End Sub optTwoPayments.Value = 0
optThreePayments.Value = 0
End Sub

Private Sub cmdCompute_Click() Private Sub cmdExit_Click()


If optCash.Value = True Then End
sglDiscount = Val(txtEnterTuition.Text) End Sub
* 0.1
sglTotal = Val(txtEnterTuition.Text) -
sglDiscount
txtTotalTuition.Text = Str(sglTotal)
ElseIf optTwoPayments.Value = True
Then
sglInterest = Val(txtEnterTuition.Text)
* 0.05
sglTotal = Val(txtEnterTuition.Text) +
sglInterest
txtTotalTuition.Text = Str(sglTotal)
ElseIf optThreePayments.Value = True
Then
sglInterest = Val(txtEnterTuition.Text)
* 0.2
sglTotal = Val(txtEnterTuition.Text) +
sglInterest
txtTotalTuition.Text = Str(sglTotal)
End If
End Sub
EXERCISE 6

CONTROLS NAME CAPTION


Form frmAssessmentSystem Tuition Fee Assessment
System
Frame fraModePayment Mode of Payment
Label 1 lblEnterTuition Enter Tuition Fee:
Text Box 1 txtEnterTuition
Command Button 1 cmdCompute Compute
Label 2 lblTotalTuition Your Total Tuition Fee:
Text Box 2 txtTotalTuition
Command Button 2 cmdExit Exit

Private Sub cmdAdd_Click() Private Sub cmdSubtract_Click()


lblOutput.Caption = Val(txtNum1.Text) + dblAns = Val(txtNum1.Text) -
Val(txtNum2.Text) Val(txtNum2.Text)
lblAns.Caption = "Sum:" lblOutput.Caption = dblAns
lblAns.Caption = "Difference:"
End Sub End Sub
Private Sub cmdMultiply_Click() Private Sub cmdDivide_Click()
dblAns = Val(txtNum1.Text) * dblAns = Val(txtNum1.Text) /
Val(txtNum2.Text) Val(txtNum2.Text)
lblOutput.Caption = dblAns lblOutput.Caption = dblAns
lblAns.Caption = "Product:" lblAns.Caption = "Quotient:"
End Sub End Sub
Private Sub cmdClear_Click() Private Sub cmdExit_Click()
txtNum1.Text = "" End
txtNum2.Text = ""
lblOutput.Caption = "" End Sub
lblAns.Caption = "Answer:"
End Sub

EXERCISE 7

CONTROLS NAME CAPTION


Form frmTempConvert Temperature Converter
Label 1 lblHeader Convert Your Temperature:
Option Button optFahrToCel Fahrenheit to Celsius
Option Button optCelToFahr Celsius to Fahrenheit
Label 2 lblTempInput
Text Box 1 txtTempInput
Label 3 lblTempOutput
Text Box 2 txtTempOutput
Command Button 1 cmdConvert Convert
Command Button 2 cmdExit Exit

Private Sub optFahrToCel_Click() Private Sub optCelToFahr_Click()


If (optFahrToCel.Value = True) Then If (optCelToFahr.Value = True) Then
lblTempInput.Caption = "Enter your lblTempInput.Caption = "Enter your
temperature in degrees fahrenheit" temperature in degrees celsius"
txtTempInput.Text = "" txtTempInput.Text = ""
txtTempInput.SetFocus txtTempInput.SetFocus
lblTempOutput.Caption = "Equivalent lblTempOutput.Caption = "Eqiuvalent
temperature in degrees celsius" temperaturein degrees in fahrenheit"
txtTempOutput.Text = "" txtTempOutput.Text = ""
End If End If
End Sub End Sub
Private Sub Form_Load() Private Sub cmdExit_Click()
optCelToFahr.Value = 0 End
optFahrToCel.Value = 0 End Sub
End Sub
Private Sub cmdConvert_Click()
Dim dblTempIn, dblTempOut As Double
dblTempIn = Val(txtTempInput.Text)
If (optFahrToCel.Value = True) Then
dblTempOut = (5 / 9) * (dblTempIn -
32)
Else
dblTempOut = 1.8 * dblTempIn + 32
End If
txtTempOutput.Text =
Str(dblTempOut)
End Sub

EXERCISE 8

CONTROLS NAME CAPTION


Form frmDateTimePicker Date – Time Picker
Label 1 lblHeader1 Your System Clock is:
Label 2 lblSystemClock
Label 3 lblHeader2 Today is:
Label 4 lblDateToday
Timer tmrTimer1

Private Sub Form_Load() Private Sub tmrTimer1_Timer()


frmDateTimePicker.Show If lblSystemClock.Caption <> CStr(Time)
lblDateToday.Caption = Format(Now, Then
"dddd,mmmm,d,yyyy") lblSystemClock.Caption = Time
End If
End Sub End Sub

EXERCISE 9

CONTROLS NAME CAPTION


Form frmGradeCalculator Grade Calculator
Label 1 lblGrade Grade:
Text Box 1 txtGrade
Label 2 lblRemarks Remarks:
Text Box 2 txtRemarks
Command Button 1 cmdCalculate Calculate
Command Button 2 cmdClear Clear
Command Button 3 cmdExit Exit

Private Sub cmdClear_Click() Private Sub cmdExit_Click()


txtGrade.Text = " " End
txtRemarks.Text = " "
txtGrade.SetFocus End Sub
End Sub
Private Sub cmdCalculate_Click()
Dim Grade As Single, Remarks As String
Grade = Val(txtGrade.Text)
Select Case Grade
Case 0 To 49
Remarks = "Fail"
Case 50 To 59
Remarks = "Pass"
Case 60 To 69
Remarks = "Medium"
Case 70 To 79
Remarks = "Good"
Case 80 To 89
Remarks = "Very Good"
Case 90 To 99
Remarks = "Excellent!"
Case Else
MsgBox "Wrong Entry, Please Re-enter
the Grade", 16, "Error"
txtGrade.Text = " ": txtRemarks.Text =
" ": Exit Sub
End Select
txtRemarks.Text = Remarks
End Sub

EXERCISE 10
CONTROLS NAME CAPTION
Form frmFibonacciSequence Fibonacci Sequence
Picture picOutput
Command Button 1 cmdForNext For Next
Command Button 2 cmdDoWhile Do While
Command Button 3 cmdDoUntil Do Until

Private Sub cmdForNext_Click() Private Sub cmdDoWhile_Click()


Dim a, b, c, series As Integer Dim a, b, c, series As Integer
picOutput.Cls picOutput.Cls
series = InputBox("Enter number series") series = InputBox("Enter number of series")
a=1 a=1
For x = 1 To series Do While x < series
If x > 2 Then If x > 1 Then
c=a+b c=a+b
a=b a=b
b=c b=c
picOutput.Print c & Space(2); picOutput.Print c & Space(2);
Else Else
picOutput.Print a & Space(2); picOutput.Print a & Space(2);
b=a b=a
End If End If
Next x x=x+1
End Sub Loop
End Sub

Private Sub cmdDoUntil_Click()


Dim a, b, c, series As Integer
picOutput.Cls
series = InputBox("Enter number of series")
a=1
Do Until x > series - 1
If x > 1 Then
c=a+b
a=b
b=c
picOutput.Print c & Space(2);
Else
picOutput.Print a & Space(2);
b=a
End If
x=x+1
Loop
End Sub

You might also like