NAME: DAISY MWANDIHI
REG: DCS/2022/36138
UNIT NAME: VISUAL/EVENT DRIVEN PROGRAMMING
UNIT CODE: DIT 2216
1. Explain 3 components of the Visual Basic IDE (6 marks)
a.) Code Editor: is where developers write and edit their Visual Basic code. It provides features
such as syntax highlighting, code completion, and error checking to aid in writing efficient and
error-free code. It's the central workspace where developers spend most of their time crafting
their applications. It simplifies the process of creating visually appealing and interactive user
interfaces.
b.) Form designer: It allows developers to design the graphical user interface (GUI) of their
applications visually. Developers can drag and drop various controls such as buttons, text boxes,
labels, etc., onto the form and then customize their properties and layout using the properties
window and layout tools. This component enables developers to create visually appealing and
functional interfaces without writing extensive code manually.
c.) Project Explorer: The Project Explorer provides an organized view of the project's structure,
including forms, modules, classes, and other resources. It allows developers to navigate through
different components of their project, manage files, and access properties and events associated
with each item. The Project Explorer is essential for managing and organizing project files and
facilitates efficient navigation and maintenance of large projects.
2. Describe 3 types control structures used in VB programming (6 marks)
Sequential Structure: It is a type of control structure where statements are executed in the
order in which they appear in the code, procedural. The flow of execution runs from the
top of the code to the bottom, executing every statement in sequential sequence.
Example:
```vb
Dim x As Integer
Dim y As Integer
x=5
y = 10
Dim z As Integer
z=x+y
```
Selection Structure : Selection structures allow the program to make decisions based on
certain conditions. In VB, this is often implemented using the If...Then...Else statement. This
structure evaluates a condition and executes different blocks of code based on whether the
condition is true or false.
Example:
```vb
Dim age As Integer
age = 25
If age >= 18 Then
MsgBox("You are an adult.")
Else
MsgBox("You are a minor.")
End If
```
Repetition Structure (Loops): Repetition structures, commonly referred to as loops, allow a
block of code to be executed repeatedly based on a certain condition. In VB, common loop
structures include the For...Next loop, Do...Loop, and While...End While loop.
Example using a For...Next loop:
```vb
For i = 1 To 5
MsgBox("Iteration number: " & i)
Next i
```
3.Design the GUI, specify the properties and write a VB program that accepts marks for 3 units
through textboxes, computes the total, average and then grades a student based on the criteria
given below using the If…ElseIf construct. The program should then use labels to display the
total mark, average and the grade. (7 marks)
AVERAGE GRADE
80-100 Distinction
70-79 Credit
60-69 Credit II
50-59 Pass
0-49 Fail
Any other Invalid Data Entry
Design the GUI:
- Place three TextBox controls named `txtUnit1`, `txtUnit2`, `txtUnit3` for entering
marks.
- Place three Label controls named `lblTotal`, `lblAverage`, `lblGrade` for displaying
the total mark, average mark, and grade.
- Place a Button control named `btnCalculate` for triggering the calculation and
grading process.
Specify the properties:
- For the TextBox controls, set the `Text` property to empty ("") initially.
- For the Label controls, set the `Text` property to empty ("") initially.
- Set the `Text` property of `btnCalculate` to "Calculate".
Write the VB program using If...ElseIf construct:
```vb
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles
btnCalculate.Click
' Declare variables to hold marks and calculate total and average
Dim mark1, mark2, mark3, total, average As Double
' Parse the marks from the TextBoxes
Double.TryParse(txtUnit1.Text, mark1)
Double.TryParse(txtUnit2.Text, mark2)
Double.TryParse(txtUnit3.Text, mark3)
' Calculate total and average
total = mark1 + mark2 + mark3
average = total / 3
' Display total and average in the corresponding Label controls
lblTotal.Text = "Total: " & total.ToString()
lblAverage.Text = "Average: " & average.ToString("0.00")
' Determine the grade based on the average mark
If average >= 80 AndAlso average <= 100 Then
lblGrade.Text = "Grade: Distinction"
ElseIf average >= 70 AndAlso average <= 79 Then
lblGrade.Text = "Grade: Credit"
ElseIf average >= 60 AndAlso average <= 69 Then
lblGrade.Text = "Grade: Credit II"
ElseIf average >= 50 AndAlso average <= 59 Then
lblGrade.Text = "Grade: Pass"
ElseIf average >= 0 AndAlso average <= 49 Then
lblGrade.Text = "Grade: Fail"
Else
lblGrade.Text = "Grade: Invalid Data Entry"
End If
End Sub
```
4. Design the GUI, specify the properties and write a VB program that accepts a number
through a textbox and then uses the Select Case construct to determine the 12
months of the year based on the number entered by the user. If a user enters 1, the
program displays January. If a user enters 6, the program displays June. Use a label for
display (4 marks)
Design the GUI:
- Place a TextBox control named `txtMonthNumber` for entering the month number.
- Place a Button control named `btnDisplayMonth` for triggering the display of
the month.
- Place a Label control named `lblMonthDisplay` to display the result.
Specify the properties:
- Set the `Text` property of `btnDisplayMonth` to "Display Month".
- Set the `Text` property of `lblMonthDisplay` to empty ("").
Write the VB program using Select Case construct:
```vb
Private Sub btnDisplayMonth_Click(sender As Object, e As EventArgs) Handles
btnDisplayMonth.Click
' Declare a variable to hold the month number entered by the user
Dim monthNumber As Integer
' Parse the month number from the TextBox
Integer.TryParse(txtMonthNumber.Text, monthNumber)
' Use Select Case to determine the month based on the number entered
Select Case monthNumber
Case 1
lblMonthDisplay.Text = "January"
Case 2
lblMonthDisplay.Text = "February"
Case 3
lblMonthDisplay.Text = "March"
Case 4
lblMonthDisplay.Text = "April"
Case 5
lblMonthDisplay.Text = "May"
Case 6
lblMonthDisplay.Text = "June”
Case 7
lblMonthDisplay.Text = "July"
Case 8
lblMonthDisplay.Text = "August"
Case 9
lblMonthDisplay.Text = "September"
Case 10
lblMonthDisplay.Text = "October"
Case 11
lblMonthDisplay.Text = "November"
Case 12
lblMonthDisplay.Text = "December"
Case Else
lblMonthDisplay.Text = "Invalid Month Number"
End Select
End Sub
```
5.Write a VB progrm tuhat generates and displays a series of 6 numbers and their
sum through message boxes. The series order is 5,11,17. The program must use a loop. (7
marks)
vb
Module Module1
Sub Main()
Dim numbers() As Integer = {5, 11, 17}
Dim sum As Integer = 0
For i = 1 To 3
sum += numbers(i - 1)
MsgBox("Number " & i & ": " & numbers(i - 1))
Next
MsgBox("The sum of the numbers is: " & sum)
End Sub
End Module