You are on page 1of 13

ANSWER SHEET OF VISUAL BASIC SENIOR FIVE

SECTION A: Attempt all question of this part /55marks

1. Differentiate terms that follow:

a. Drive List Box: The drive list box control allows a user to select a valid disk
drive at run-time. It displays the available drives in a drop-down combo box.
b. Directory List Box: The directory list box displays an ordered, hierarchical list
of the user's disk directories and subdirectories. The directory structure is
displayed in a list box.
c. File List Box: The file list box locates and lists files in the directory specified by
its Path property at run-time. You may select the types of files you want to
display in the file list box.

2. Give the role of the property Path which is used on Directory List Box and Directory List Box

Path: Contains the current directory path. 5marks

3. Give an example which uses path property. 5marks

Private Sub drvImage_Change ()


'If drive changes, update directory
dirImage.Path = drvImage.Drive
End Sub

Private Sub dirImage_Change()


'If directory changes, update file path
filImage.Path = dirImage.Path
End Sub

4. Give at least two events of File List Box. 10marks

DblClick: Triggered whenever a file name is double-clicked.


PathChange: Triggered whenever the path changes in a file list box.

5. Given the interface that follow: 10marks


a. Give the process to insert menu on a form
Select menu editor from tools menu then write the caption and name of a new menu then click
ok button.

b. How to underline characters which are underlined?


We place & symbol before a character to underline.
c. How to put the line under close?
We use an underscore
d. What is the event of menu or sub-menu?
The event of menu or sub-menu is click.
e. Write a code to close the form after clicking close sub-menu.
Private Sub mnuclose_Click()
Unload Me
End Sub

6. What will be the output of this code if the user input 8. /5marks
Private Sub cmdStart_Click()
       Dim x As Single, y As Single
       x = Val(txtX.Text)
        y = x^2 + 1
       picResults.Cls
        picResults.Print “x = ”, x
       picResults.Print “y = ”, y

End Sub
Answer:
X=8
Y=65

7. What do you understand by: /5marks


a. Event procedure:
Commands executed when an event occurs during program execution.
An event procedure is a piece of code that you write to execute a
programming event. For example, when a user clicks a CommandButton, a
Click() event is generated. Visual Basic provides a CommandButton_Click()
event procedure that you can program to react to the CommandButton click.
b. Controls:
Controls are tools such as boxes, buttons, labels and other objects draw on a
form to get input or display output. They also add visual appeal.
8. Give the general format of the following: /5marks
a. while loop:
While logical expression
            .  .  .  .  .  .
            executable statements
            .  .  .  .  .  .  .
Wend
b. for:
For index = value1 To value2
            .   .   .   .   .   .   .
            executable statements
            .   .   .   .   .   .   .
Next index
or
For counter=startNumber to endNumber (Step increment)
    One or more VB statements
Next counter
9. What are steps followed in building a visual basic application? /5marks
Answer:

 Step 1 : Design the interface


Step 2 : Set properties of the controls (Objects)
Step 3 : Write the event procedures

10.What are the main differences between CheckBoxes and OptionButtons.


/5marks
Answer:

Check boxes versus Option buttons


These two controls are used when the user must choose from a list of options. The
difference between the two is that with Check boxes he can select several at one
time but, with Option buttons, he must select only one among several.

The example below illustrates the use of Check boxes and Option buttons. To
capture the information entered by means of these controls, you must test the
property: Value. In a Check box, Value = 1 if box is checked and = 0 if not. In an
Option button, Value = True if selected and = False if not.

11.Which one of the following is the correct syntax to make a control called Pic1
visible?
a. Pic1.Visible = True
b. Visible.Pic1 = True
c. Pic1.Invisible = False
d. Pic1.Enabled = True

SECTION B: Attempt any three questions /30marks

1. Write a vb program to respond to the following form : /10marks


Note: Number of hours must be entered as well as the appropriate rate. Gross salary
= rate * hours. Net salary = gross salary - deductions.
Answer:
Private Sub cmdCancel_Click()
Form_Load
End Sub
Private Sub cmdConfirm_Click()
Dim name As String
Dim hrs As Single, deduction As Single
Dim rate As Single, gross As Single, net As Single
name = txtEmpName.Text
hrs = Val(txtHrs.Text)
deduction = Val(txtAmount.Text)
If optRateA.Value = True Then
rate = 10
ElseIf optRateB.Value = True Then
rate = 12.5
ElseIf optRateC.Value = True Then
rate = 15
End If
gross = hrs * rate
net = gross - deduction
lblGross.Caption = Str(gross)
lblNet.Caption = Str(net)
End Sub
Private Sub cmdExit_Click()
End
End Sub
Private Sub Form_Load()
txtEmpName.Text = ""
txtHrs.Text = ""
txtAmount.Text = ""
lblGross.Caption = ""
lblNet.Caption = ""
optRateA.Value = False
optRateB.Value = False
optRateC.Value = False
End Sub
2. Write a vb program that will respond to the user action from this form: /10marks

Answer:

Private Sub chkBold_Click()


If chkBold.Value = 1 Then
txtDisplay.FontBold = True
Else
txtDisplay.FontBold = False
End If
End Sub
Private Sub chkItalic_Click()
If chkItalic.Value = 1 Then
txtDisplay.FontItalic = True
Else
txtDisplay.FontItalic = False
End If
End Sub
Private Sub chkUnderline_Click()
If chkUnderline.Value = 1 Then
txtDisplay.FontUnderline = True
Else
txtDisplay.FontUnderline = False
End If
End Sub
Private Sub mcdExit_Click()
End
End Sub
Private Sub opt12_Click()
txtDisplay.FontSize = 12
End Sub
Private Sub opt14_Click()
txtDisplay.FontSize = 14
End Sub
Private Sub opt24_Click()
txtDisplay.FontSize = 24
End Sub
Private Sub optBlue_Click()
txtDisplay.ForeColor = vbBlue
End Sub
Private Sub optGreen_Click()
txtDisplay.ForeColor = vbGreen
End Sub
Private Sub optRed_Click()
txtDisplay.ForeColor = vbRed
End Sub
3. Examine the following Menu Editor and answer the questions

a)What are two most important properties of Menu Editor? Explain

 Name – This is the name you use to reference the menu editor

 Caption – This is a text which will appear on control menu or sub menus

b)Give a note for the following buttons on Menu Editor:

i. Insert – It insert a line into control list above the selected line

ii. Next – It moves the selected menu to the next line

iii. Delete – it delete the selected line

iv. Ok – it is a button that closes the menu editor and makes all modification
done to the selected form

4. Present at least in three ways of how the Select case can be used. And give an
example for each way. /10marks
Answer:
Select...Case structure is an alternative to If...Then...ElseIf for selectively executing a
single block of statements from among multiple block of statements. Select...case is
more convenient to use than the If...Else...End If.
- The select case visual basic command takes the place of multiple nested if
statements and makes your VB code clean and much easier to follow by neatly
handling the conditional flow of your VBA programming code.

Select Case expression


   Case value1
        Block of one or more VB statements
   Case value2
        Block of one or more VB Statements
   Case value3
            .
        .
   Case Else
        Block of one or more VB Statements
End Select
1 . Dim grade As String
Private Sub Compute_Click( )
grade=txtgrade.Text
Select Case grade
  Case  "A"
       lblresult.Caption="High  Distinction"
  Case "A-"
      lblresult.Caption="Distinction"
  Case "B"
       lblresult.Caption="Credit"
  Case "C"
        lblresult.Caption="Pass"
  Case Else
        lblresult.Caption="Fail"
  End Select
End Sub
2. Dim mark As Single
Private Sub Compute_Click()
'Examination Marks
 mark = txtmrk.Text
  Select Case mark
 Case Is >= 85
      lblcomment.Caption = "Excellence"
Case Is >= 70
     lblcomment.Caption = "Good"
 Case Is >= 60
   lblcomment.Caption = "Above Average"
 Case Is >= 50
lblcomment.Caption = "Average"
 Case Else
lblcomment.Caption = "Need to work harder"
 End Select
 End Sub

3. Example 2 could be rewritten  as follows:


 Dim mark As Single
Private Sub Compute_Click()
'Examination Marks
 mark = txtmrk.Text
 Select Case mark
 Case 0 to 49
      lblcomment.Caption = "Need to work harder"
 Case 50 to 59
    lblcomment.Caption = "Average"
 Case 60 to 69
  lblcomment.Caption = "Above Average"
 Case 70 to 84
lblcomment.Caption = "Good"
 Case Else
lblcomment.Caption = "Excellence"
 End Select
 End Sub

5. Following the steps given to build a Visual Basic application, solve the problem of
displaying in a label the square root of the number supplied by a user.
/10marks
Answer:
Step 1 : Design the interface

Step 2 : Set properties

Object Property setting


Form Caption display the square root of a positive
number
Name frmSquareroot
Text Text “”
Name txtNum
Label1 Caption Enter the number:
Name lblNUm
CommandButto Caption Square root
n Name smdResult
Label2 Caption “”
Name lblSqroot

Step 3 : Write the event procedures


Private Sub smdResult_Click()
Dim num As Single, squareroot As Single
num = Val(txtNum.Text)
squareroot = Sqr(num)
lblSqroot = "The square root of " & str(num) & " is " &str( squareroot)
End Sub

SECTION C: Attempt any one question /15marks

1. Write a vb program that allows the user to input the starting and end values. The
program calculates and displays the sum of the numbers between these values.
Note: the start and end values are included. /15marks
Answer:
Private Sub cmdSum_Click()
Dim num1 As Single, num2 As Single, sum As Single
Dim i As Integer
sum = 0
picSum.Cls
listSum.clear
num1 = Val(txtNum1.Text)
num2 = Val(txtNum2.Text)
For i = num1 To num2
sum = sum + i
Next i
1. picSum.Print "The sum from "; num1; " to "; num2; " is "; sum
2. lstSum.AddItem sum
End Sub
2. Private Sub cmdDisplay_Click()
Dim num(25) as single
Dim sum as single, temp as single
Dim i as integer
pictorder.cls
lstvalue.clear
for i=1 to 25
num(i)=inputbox(“enter the value of number ”,i)
next i
rem displaying the values
for i=1 to 25
lstvalue.AddItem stud(i)
next i
rem ordering the numbers
for i=1 to 24
for i=i+1 to 25
if(num(i)>num(j)) then
temp=num(i)
num(i)=num(j)
num(j)-temp
end if
next j
next i
rem displaying ordered numbers
for i=1 to 25
picoorder.print num(i)
next i
rem calculation of the sum
sum= 0
for i=1 to 25
sum=sum + num(i)
next i
lblsum. Caption= “Their sum is” & sum
End sub 
1.

You might also like