0% found this document useful (0 votes)
18 views23 pages

Visual Basic Programming Concepts Explained

The document outlines various aspects of Visual Basic (VB) programming, including components of the Microsoft Visual Studio IDE, differences between procedural and object-oriented programming, and definitions of key programming concepts. It also provides examples of VB code for calculating areas, handling events, and using controls in GUI design. Additionally, it covers error types, variable scopes, and the benefits of using functions in VB projects.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views23 pages

Visual Basic Programming Concepts Explained

The document outlines various aspects of Visual Basic (VB) programming, including components of the Microsoft Visual Studio IDE, differences between procedural and object-oriented programming, and definitions of key programming concepts. It also provides examples of VB code for calculating areas, handling events, and using controls in GUI design. Additionally, it covers error types, variable scopes, and the benefits of using functions in VB projects.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Event Planning

QUESTION ONE

a) Three Components of Microsoft Visual Studio IDE (6 Marks)

1. Editor:
o Explanation: The editor is where developers write and modify their code. It
supports syntax highlighting, IntelliSense (code completion), and debugging
tools, making the coding process more efficient and error-free.
o Key Features: Code highlighting, code suggestions, error detection, and line
numbers.
2. Solution Explorer:
o Explanation: The Solution Explorer helps developers organize and manage their
projects and files within Visual Studio. It shows a hierarchical view of the
projects and their components, making it easy to navigate between different parts
of a project.
o Key Features: Project hierarchy, file management, and easy navigation.
3. Debugger:
o Explanation: The debugger allows developers to run their code step-by-step to
identify and fix errors. It provides breakpoints, watch windows, and call stack
analysis to help locate and resolve issues in the code.
o Key Features: Breakpoints, step-through execution, variable inspection, and call
stack tracing.

b) Difference between Procedural and Object-Oriented Programming (4 Marks)

 Procedural Programming:
o Definition: Procedural programming is a programming paradigm based on the
concept of procedure calls, where programs are structured as a sequence of
procedures or functions.
o Characteristics: Emphasizes functions, uses a top-down approach, and the main
focus is on procedures that operate on data.
o Example: C, Pascal.
 Object-Oriented Programming (OOP):
o Definition: OOP is a programming paradigm based on the concept of objects,
which can contain data and code to manipulate that data. Objects are instances of
classes.
o Characteristics: Emphasizes objects and classes, uses a bottom-up approach,
supports inheritance, encapsulation, and polymorphism.
o Example: Java, C++, Python.

c) Description of Terms (6 Marks)


 Event Handler:
o Description: An event handler is a function or method that is called in response to
an event. Events are actions or occurrences detected by the program, such as
clicks, key presses, or mouse movements.
o Example: In VB, an event handler for a button click might be Button1_Click.
 Event-Driven Program:
o Description: An event-driven program is a program designed to respond to user
or system events. The flow of the program is determined by events, with event
handlers responding to these events.
o Example: GUI applications where user interactions (clicks, typing) trigger
specific code execution.
 Events:
o Description: Events are actions or occurrences that happen in a program and can
be detected by the program. Events are generated by user actions, like clicking a
button, or by system-generated events, like a timer.
o Example: A Click event for a button, a TextChanged event for a textbox.

d) VB Program for Calculating the Area of a Circle (5 Marks)

vb
Copy code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
[Link]
Dim radius As Double = 24
Dim pie As Double = 3.142
Dim area As Double = pie * radius * radius
[Link] = [Link]()
End Sub
End Class

e) VB Code to Illustrate Variable Declaration (4 Marks)

vb
Copy code
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles
[Link]
Dim age As Integer
Dim name As String
Dim pi As Double
Dim isActive As Boolean
End Sub
End Class

f) VB Program to Display a Welcome Message (5 Marks)

vb
Copy code
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles
[Link]
[Link]("Welcome to visual basic programming")
End Sub
End Class

QUESTION TWO

a) Structure of VB Sub Procedure (4 Marks)

A sub procedure in VB is a block of code that performs a specific task. It does not return a value
and is defined using the Sub keyword. The structure includes the procedure declaration, code
block, and the End Sub statement.

Example:

vb
Copy code
Public Sub CalculateSum(ByVal num1 As Integer, ByVal num2 As Integer)
Dim sum As Integer
sum = num1 + num2
[Link]("The sum is " & sum)
End Sub

b) Types of Variables (6 Marks)

 Double:
o Description: A Double variable is used to store floating-point numbers with
double precision. It can store large and small numbers with decimal points.
o Example: Dim distance As Double = 12345.6789
 Integer:
o Description: An Integer variable is used to store whole numbers without
decimal points. It is suitable for counting and indexing.
o Example: Dim count As Integer = 100
 String:
o Description: A String variable is used to store sequences of characters, such as
words or sentences.
o Example: Dim message As String = "Hello, World!"

c) [Link] Program for Calculating Area of a Rectangle (6 Marks)

vb
Copy code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
[Link]
Dim length As Double = [Link]([Link])
Dim width As Double = [Link]([Link])
Dim area As Double = length * width
[Link] = "Area: " & [Link]()
End Sub
End Class

d) Difference between an Object and a Class in VB (4 Marks)

 Class:
o Definition: A class is a blueprint for creating objects. It defines properties,
methods, and events for the objects.
o Example: A Car class with properties like color and model, and methods like
drive and stop.
 Object:
o Definition: An object is an instance of a class. It is created based on the structure
defined by the class.
o Example: A specific car, like myCar, created from the Car class.

QUESTION THREE

a) Three Types of Errors in Visual Basic Programming (6 Marks)

1. Syntax Errors:
o Description: Errors that occur when the code violates the grammatical rules of
the programming language. These errors are detected by the compiler or
interpreter.
o Example: Missing a semicolon or a mismatched parenthesis.
2. Runtime Errors:
o Description: Errors that occur while the program is running. These are typically
due to illegal operations, such as dividing by zero or accessing an array out of
bounds.
o Example: Trying to open a file that does not exist.
3. Logical Errors:
o Description: Errors that occur when the program runs without crashing but
produces incorrect results. These are often due to flawed logic or incorrect
algorithm implementation.
o Example: Incorrectly calculating the sum of numbers.

b) Definition of Object (2 Marks)

An object is an instance of a class that encapsulates data and behavior. It represents a real-world
entity with attributes (properties) and actions (methods).

c) Variables in Visual Basic Programming (12 Marks)

i. Definition of a Variable (2 Marks): A variable is a storage location in memory with a name


and a data type, used to store data that can be changed during program execution.

ii. Four Rules for Naming Variables in VB (4 Marks):


1. Must begin with a letter.
2. Can include letters, numbers, and underscores.
3. Cannot include spaces or special characters.
4. Cannot be a reserved keyword.

iii. Three Ways to Express Variable Scope (6 Marks):

1. Local Scope:
o Description: Variables declared within a procedure or function. Accessible only
within that procedure or function.
o Example: Dim localVar As Integer
2. Global Scope:
o Description: Variables declared at the module or class level with the Public
keyword. Accessible from any procedure within the module or class.
o Example: Public globalVar As Integer
3. Module-Level Scope:
o Description: Variables declared at the module level with the Private keyword.
Accessible only within that module.
o Example: Private moduleVar As Integer

QUESTION FOUR

a) Difference between String Constants and Numeric Constants (4 Marks)

 String Constants:
o Definition: Constants that contain sequences of characters enclosed in quotation
marks.
o Example: "Hello, World!"
 Numeric Constants:
o Definition: Constants that represent numeric values, which can be integers or
floating-point numbers.
o Example: 3.142 or 100

b) Three Types of Operators Used in VB (6 Marks)

1. Arithmetic Operators:
o Description: Perform mathematical operations on numeric values.
o Example: +, -, *, /, %
o Usage: Dim sum As Integer = a + b
2. Relational Operators:
o Description: Compare two values and return a Boolean result.
o Example: =, <>, <, >, <=, >=
o Usage: If a > b Then
3. Logical Operators:
o Description: Combine multiple Boolean expressions and return a Boolean result.
o Example: And, Or, Not
o Usage: If a > b And c < d Then

c) VB Form Application to Calculate Area of Rectangle (6 Marks)

vb
Copy code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
[Link]
Dim length As Double = [Link]([Link])
Dim width As Double = [Link]([Link])
Dim area As Double = length * width
[Link] = "Area: " & [Link]()
End Sub
End Class

d) Definition and Syntax of Arrays (4 Marks)

An array is a collection of elements of the same type, stored in contiguous memory locations,
and accessible using an index.

Syntax:

vb
Copy code
Dim arrayName(size) As DataType

Example:

vb
Copy code
Dim numbers(5) As Integer

QUESTION FIVE

a) Definition of Function (4 Marks)

A function is a block of code that performs a specific task, takes input parameters, and returns a
value. Functions promote code reuse and modularity.

b) Syntax of Defining a Function in VB Project (4 Marks)

Example:

vb
Copy code
Public Function CalculateSum(ByVal num1 As Integer, ByVal num2 As Integer) As
Integer
Dim sum As Integer
sum = num1 + num2
Return sum
End Function

c) Benefits of Using Functions in a VB Project (4 Marks)

1. Code Reusability: Functions allow code to be reused in multiple places, reducing


redundancy.
2. Modularity: Functions break down complex problems into smaller, manageable pieces.
3. Maintainability: Functions make code easier to read, understand, and maintain.
4. Testing: Functions can be tested independently, improving the reliability of the code.

d) Three Standard Controls in VB (3 Marks)

1. Button: Used to perform an action when clicked.


2. TextBox: Used to accept input from the user.
3. Label: Used to display text on the form.

e) Program to Display Day of the Week Based on User Input (7 Marks)

vb
Copy code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
[Link]
Dim dayNumber As Integer = [Link]([Link])
Dim dayName As String

Select Case dayNumber


Case 1
dayName = "Sunday"
Case 2
dayName = "Monday"
Case 3
dayName = "Tuesday"
Case 4
dayName = "Wednesday"
Case 5
dayName = "Thursday"
Case 6
dayName = "Friday"
Case 7
dayName = "Saturday"
Case Else
dayName = "Invalid day number"
End Select

[Link](dayName)
End Sub
End Class
4o

QUESTION ONE
a) Five Controls Used in VB Interface Design (10 Marks)

1. Button Control:
o Explanation: The Button control is used to perform an action when clicked by
the user.
o Properties: Text (sets the label on the button), Click event (defines what
happens when the button is clicked).
o Example: [Link] = "Submit"
2. TextBox Control:
o Explanation: The TextBox control is used to accept user input in the form of text.
o Properties: Text (gets or sets the current text in the textbox), MaxLength (limits
the number of characters that can be entered).
o Example: [Link] = "Enter your name"
3. Label Control:
o Explanation: The Label control is used to display static text or descriptive
information to the user.
o Properties: Text (sets the text to be displayed), AutoSize (adjusts the size of the
label to fit its text).
o Example: [Link] = "Name"
4. ComboBox Control:
o Explanation: The ComboBox control is used to display a drop-down list from
which the user can select one item.
o Properties: Items (holds the items in the combo box), SelectedItem (gets or
sets the currently selected item).
o Example: [Link]("Option1")
5. ListBox Control:
o Explanation: The ListBox control is used to display a list of items from which
the user can select one or more.
o Properties: Items (holds the items in the list box), SelectedItem (gets or sets
the currently selected item).
o Example: [Link]("Item1")

b) Difference Between Design Time and Run Time (4 Marks)

 Design Time:
o Definition: Design time is when you create and design the user interface and
write the code in the development environment. This is the phase where you
define the look and feel of the application.
o Example: Setting properties of controls, arranging controls on the form, and
writing event handlers.
 Run Time:
o Definition: Run time is when the application is executed, and the code is run.
This is the phase where the user interacts with the application.
o Example: Clicking a button to perform an action, entering text in a TextBox, and
the code responding to these actions.
c) Difference Between Name and Text Properties (4 Marks)

 Name Property:
o Definition: The Name property is used to identify the control in code. It is a
unique identifier within the form.
o Usage: Referencing the control in the code to set properties or handle events.
o Example: [Link] = "txtName"
 Text Property:
o Definition: The Text property is used to display text within the control or get the
text entered by the user.
o Usage: Displaying or retrieving the content of the control.
o Example: [Link] = "Enter your name"

d) MsgBox and InputBox Functions (4 Marks)

 MsgBox Function:
o Definition: The MsgBox function displays a message box to the user with a
specified message and buttons.
o Syntax: MsgBox(prompt As String, [buttons As MsgBoxStyle], [title
As String])
o Example: MsgBox("Operation successful!", [Link],
"Status")
 InputBox Function:
o Definition: The InputBox function displays a dialog box that prompts the user to
enter text.
o Syntax: InputBox(prompt As String, [title As String],
[defaultResponse As String], [xPos As Integer], [yPos As Integer])
o Example: Dim userInput As String = InputBox("Enter your name:",
"User Input")

e) Design GUI and Write Code in [Link] (6 Marks)

i) Add Items to ListBox (3 Marks)

 GUI Design:
o TextBox: TextBox1
o Button: Button1
o ListBox: ListBox1
 Code:

vb
Copy code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
[Link]
[Link]([Link])
[Link]()
End Sub
ii) Remove Selected Item from ListBox (3 Marks)

 GUI Design:
o Button: Button2
o ListBox: ListBox1
 Code:

vb
Copy code
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles
[Link]
[Link]([Link])
End Sub

f) Variable Declaration in [Link] (2 Marks)

Variables in [Link] are declared using the Dim keyword followed by the variable name and
type.

Example:

vb
Copy code
Dim age As Integer
Dim name As String
Dim isStudent As Boolean

QUESTION TWO

a) Components of the .NET Platform and Architecture of the .NET Framework (10 Marks)

 Common Language Runtime (CLR):


o Description: The CLR is the execution engine for .NET applications. It provides
services such as memory management, garbage collection, security, and exception
handling.
 Base Class Library (BCL):
o Description: The BCL provides a set of standard libraries that developers can use
to perform common tasks such as file I/O, string manipulation, data collection,
and more.
 [Link]:
o Description: [Link] is a web framework for building web applications and
services with .NET. It includes libraries for web page templating, authentication,
and state management.
 [Link]:
o Description: [Link] is a set of classes that provides access to data sources
such as databases and XML files. It includes components for connecting to
databases, executing commands, and managing data.
 Windows Forms:
o Description: Windows Forms is a GUI framework for building desktop
applications on Windows. It provides a wide range of controls and components
for building rich user interfaces.

Diagram:

markdown
Copy code
-------------------------------------
| .NET Framework |
-------------------------------------
| Common Language Runtime |
|-----------------------------------|
| Base Class Library (BCL) |
|-----------------------------------|
| [Link] | [Link] | Windows Forms |
-------------------------------------

b) Design GUI and Write Code for [Link] Application (10 Marks)

i) Accept Length & Height in TextBoxes

 GUI Design:
o TextBox for Length: TextBox1
o TextBox for Height: TextBox2
o Button: Button1

ii) Calculate Area of Rectangle

iii) Display Result in MsgBox

 Code:

vb
Copy code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
[Link]
Dim length As Double = [Link]([Link])
Dim height As Double = [Link]([Link])
Dim area As Double = length * height
MsgBox("The area of the rectangle is " & [Link]())
End Sub
End Class

QUESTION THREE

a) Design Application to Calculate Simple Interest (10 Marks)


 GUI Design:
o TextBox for Principal: TextBox1
o TextBox for Rate: TextBox2
o TextBox for Years: TextBox3
o Button: Button1
 Code:

vb
Copy code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
[Link]
Dim principal As Double = [Link]([Link])
Dim rate As Double = [Link]([Link])
Dim years As Double = [Link]([Link])
Dim simpleInterest As Double = (principal * rate * years) / 100
MsgBox("The Simple Interest is " & [Link]())
End Sub
End Class

b) VB Program to Solve Quadratic Equations (10 Marks)

 GUI Design:
o TextBox for a: TextBox1
o TextBox for b: TextBox2
o TextBox for c: TextBox3
o Button: Button1
 Code:

vb
Copy code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
[Link]
Dim a As Double = [Link]([Link])
Dim b As Double = [Link]([Link])
Dim c As Double = [Link]([Link])

Dim discriminant As Double = (b * b) - (4 * a * c)


If discriminant < 0 Then
MsgBox("No Real Solutions")
Else
Dim root1 As Double = (-b + [Link](discriminant)) / (2 *
a)
Dim root2 As Double = (-b - [Link](discriminant)) / (2 *
a)
MsgBox("Roots are: " & [Link]() & " and " &
[Link]())
End If
End Sub
End Class
QUESTION FOUR

a) Definitions (10 Marks)

1. Variable:
o Definition: A storage location identified by a memory address and a symbolic
name (an identifier), which contains some known or unknown quantity of
information referred to as a value.
2. Common Language Runtime (CLR):
o Definition: The CLR is the execution engine for .NET applications, providing
services such as memory management, garbage collection, security, and exception
handling.
3. Object:
o Definition: An instance of a class that can contain data and methods to perform
actions.
4. Exception:
o Definition: An exception is an error that occurs during the execution of a
program, disrupting the normal flow of instructions.
5. Common Type System (CTS):
o Definition: CTS defines how types are declared, used, and managed in the
runtime, ensuring that objects written in different .NET languages can interact
with each other.

b) Integrated Development Environment (IDE) Components (10 Marks)

1. Solution Explorer:
o Explanation: Displays the hierarchical view of the projects and files in the
solution. It allows managing the projects, files, and references.
2. Toolbox Window:
o Explanation: Contains a collection of controls and components that can be
dragged and dropped onto the designer surface to build the user interface.
3. Properties Window:
o Explanation: Displays properties of the selected control or component, allowing
developers to set values and configure behavior.
4. Document Window:
o Explanation: The main area where code files, designers, and other documents are
opened and edited.
5. Menu Bar:
o Explanation: Provides access to various commands and features of the IDE, such
as file operations, editing, project management, and debugging tools.

QUESTION FIVE

a) Difference Between Design Time and Run Time (4 Marks)


 Design Time:
o Definition: When you create and design the user interface and write the code in
the development environment.
o Example: Setting properties of controls, arranging controls on the form, writing
event handlers.
 Run Time:
o Definition: When the application is executed and the code is run.
o Example: Clicking a button to perform an action, entering text in a TextBox, and
the code responding to these actions.

b) Six Data Types in [Link] (6 Marks)

1. Integer:
o Description: Represents whole numbers.
o Example: Dim age As Integer
2. Double:
o Description: Represents floating-point numbers.
o Example: Dim temperature As Double
3. String:
o Description: Represents a sequence of characters.
o Example: Dim name As String
4. Boolean:
o Description: Represents true or false values.
o Example: Dim isActive As Boolean
5. Date:
o Description: Represents date and time.
o Example: Dim currentDate As Date
6. Char:
o Description: Represents a single Unicode character.
o Example: Dim letter As Char

c) [Link] Program to Calculate Difference of Two Numbers (10 Marks)

 GUI Design:
o TextBox for Number 1: TextBox1
o TextBox for Number 2: TextBox2
o TextBox for Result: TextBox3
o Button: Button1
 Code:

vb
Copy code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
[Link]
Dim num1 As Double = [Link]([Link])
Dim num2 As Double = [Link]([Link])
Dim result As Double = CalculateDifference(num1, num2)
[Link] = [Link]()
End Sub

Private Function CalculateDifference(ByVal a As Double, ByVal b As


Double) As Double
Return a - b
End Function
End Class
4o

a) Definitions (10 Marks)

i) Event-procedure:

 Definition: An event-procedure is a block of code that is executed in response to a


specific event triggered by a control or object in a program.
 Example: Code that runs when a button is clicked, like:

vb
Copy code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
[Link]
' Code to execute when button is clicked
End Sub

ii) Managed Code:

 Definition: Managed code is code that runs under the control of the Common Language
Runtime (CLR) in the .NET framework, providing services like garbage collection,
exception handling, and type safety.
 Example: Code written in languages like [Link] or C# that compiles to Intermediate
Language (IL) and runs on the CLR.

iii) Array:

 Definition: An array is a data structure that can store a fixed-size sequential collection of
elements of the same type.
 Example: Declaring and using an array in [Link]:

vb
Copy code
Dim numbers(4) As Integer
numbers(0) = 10
numbers(1) = 20

iv) Enumeration:

 Definition: An enumeration is a distinct type that consists of a set of named constants


called the enumerator list.
 Example: Defining and using an enumeration in [Link]:

vb
Copy code
Enum Days
Sunday
Monday
Tuesday
End Enum
Dim today As Days = [Link]

v) Property:

 Definition: A property is a member that provides a flexible mechanism to read, write, or


compute the value of a private field.
 Example: Declaring a property in [Link]:

vb
Copy code
Public Class Person
Private _name As String
Public Property Name As String
Get
Return _name
End Get
Set(value As String)
_name = value
End Set
End Property
End Class

b) Option Strict vs. Option Explicit (4 Marks)

 Option Strict:
o Definition: Enforces strict data type conversions, disallowing implicit data type
conversions that could lead to data loss or runtime errors.
o Usage: Helps catch potential type conversion errors at compile time.
o Example:

vb
Copy code
Option Strict On
Dim num As Integer = 123.45 ' This will cause a compile-time error

 Option Explicit:
o Definition: Requires explicit declaration of all variables before they are used.
o Usage: Helps prevent errors caused by typographical errors in variable names.
o Example:

vb
Copy code
Option Explicit On
Dim num As Integer
num = 123 ' This is valid
num = "Hello" ' This will cause a compile-time error

c) DataSet vs. DataReader (4 Marks)

 DataSet:
o Definition: A DataSet is an in-memory representation of data that can hold
multiple tables and relationships between them.
o Usage: Suitable for disconnected data access and manipulation.
o Example: Used in applications where data needs to be cached for offline
processing.
 DataReader:
o Definition: A DataReader is a forward-only, read-only stream of data from a
database.
o Usage: Suitable for quickly reading data from a database without the overhead of
a DataSet.
o Example: Used in scenarios where only sequential data access is required, such
as reading through query results.

d) Common Controls in [Link] (2 Marks)

 TextBox: Used for inputting text.


 Button: Used to perform actions when clicked.
 Label: Used to display text.
 ComboBox: Used to display a drop-down list of items.
 ListBox: Used to display a list of items from which a user can select.

e) Grading System Program for Mount Kenya University (10 Marks)

GUI Design:

 TextBox for entering marks: TextBox1


 Button to calculate grade: Button1
 Label to display grade: Label1

Code:

vb
Copy code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
[Link]
Dim marks As Integer = [Link]([Link])
Dim grade As String

If marks < 40 Then


grade = "Fail"
ElseIf marks < 59 Then
grade = "Pass"
ElseIf marks < 79 Then
grade = "Merit"
ElseIf marks >= 80 Then
grade = "Distinction"
End If

[Link] = "Grade: " & grade


End Sub
End Class

QUESTION TWO (20 Marks)

a) Define IDE and Its Components (10 Marks)

 IDE (Integrated Development Environment):


o Definition: An IDE is a software application that provides comprehensive
facilities to computer programmers for software development, including writing,
testing, and debugging code.
 Components of an IDE:

1. Solution Explorer:
 Description: Displays the hierarchical view of the projects and files in the
solution, allowing for project and file management.
 Example: Adding, removing, and organizing files and references within a
project.
2. Toolbox Window:
 Description: Contains a collection of controls and components that can be
dragged and dropped onto the designer surface to build the user interface.
 Example: Adding buttons, textboxes, labels, etc., to a form.
3. Properties Window:
 Description: Displays properties of the selected control or component,
allowing developers to set values and configure behavior.
 Example: Changing the text of a label or setting the background color of a
form.
4. Document Window:
 Description: The main area where code files, designers, and other
documents are opened and edited.
 Example: Writing and editing the source code or designing a form layout.
5. Output Window:
 Description: Displays output messages from the build process, debugging,
and other IDE activities.
 Example: Viewing build errors, debugging output, and status messages.

b) Combo Box Properties, Events, and Methods (6 Marks)


 Definition: A ComboBox is a control that combines a TextBox with a ListBox, allowing
users to select an item from a drop-down list or enter their own text.
 Properties:
1. Items: Collection of items in the ComboBox.
 Example: [Link]("Item1")
2. SelectedItem: The currently selected item in the ComboBox.
 Example: Dim selected As String =
[Link]()
3. Text: The text associated with the ComboBox.
 Example: [Link] = "Select an item"
 Events:
1. SelectedIndexChanged: Occurs when the selected item changes.
 Example:

vb
Copy code
Private Sub ComboBox1_SelectedIndexChanged(sender As Object,
e As EventArgs) Handles [Link]
MsgBox("Selected item changed to: " &
[Link]())
End Sub

 Methods:
1. Add(): Adds an item to the ComboBox.
 Example: [Link]("NewItem")
2. Remove(): Removes an item from the ComboBox.
 Example: [Link]("Item1")

c) Event Procedure to Display Message on Label1 (4 Marks)

 Code:

vb
Copy code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
[Link]
[Link] = "I love visual programming"
End Sub

QUESTION THREE (20 Marks)

a) MsgBox Function Arguments (6 Marks)

 Prompt:
o Description: The message to be displayed in the message box.
o Example: MsgBox("Hello, World!")
 Style Value:
o Description: Specifies the buttons and icons to be displayed in the message box.
o Example: MsgBox("Are you sure?", [Link])
 Title:
o Description: The text to be displayed in the title bar of the message box.
o Example: MsgBox("Error occurred", [Link], "Error")

b) Built-in Date Functions (4 Marks)

 DateAdd:
o Description: Adds a specified time interval to a date.
o Example: DateAdd([Link], 5, Now) adds 5 days to the current
date.
 DateDiff:
o Description: Returns the difference between two dates.
o Example: DateDiff([Link], #1/1/2024#, #1/10/2024#)
calculates the number of days between January 1, 2024, and January 10, 2024.

c) Arrays in VB (10 Marks)

 Definition: An array is a data structure that can store a fixed-size sequential collection of
elements of the same type.
 Declaration:
o Example:

vb
Copy code
Dim numbers(49) As Double

 Program to Compute Total Cost of 50 Items:

vb
Copy code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
[Link]
Dim costs(49) As Double
Dim totalCost As Double = 0

For i As Integer = 0 To 49
costs(i) = CDbl(InputBox("Enter the cost of item " & (i +
1).ToString()))
totalCost += costs(i)
Next

MsgBox("The total cost of 50 items is: " & [Link]())


End Sub
End Class

QUESTION FOUR (20 Marks)


a) Object-related Concepts in Visual Programming (10 Marks)

1. Class:
o Description: A blueprint for creating objects, providing initial values for state
(member variables) and implementations of behavior (member functions or
methods).
o Example: Public Class Car End Class
2. Object:
o Description: An instance of a class, containing actual values and behaviors
defined by the class.
o Example: Dim myCar As New Car()
3. Inheritance:
o Description: A mechanism by which one class can inherit the properties and
methods of another class.
o Example: Public Class ElectricCar Inherits Car End Class
4. Polymorphism:
o Description: The ability of different classes to be treated as instances of the same
class through inheritance.
o Example: Dim myVehicle As Car = New ElectricCar()
5. Encapsulation:
o Description: The bundling of data with the methods that operate on that data,
restricting direct access to some of the object's components.
o Example: Using private variables and public properties:

vb
Copy code
Public Class Car
Private _speed As Integer
Public Property Speed As Integer
Get
Return _speed
End Get
Set(value As Integer)
_speed = value
End Set
End Property
End Class

b) BMI Calculator Program (10 Marks)

GUI Design:

 TextBox for mass: TextBox1


 TextBox for height: TextBox2
 Button to calculate BMI: Button1
 Label to display BMI: Label1

Code:
vb
Copy code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
[Link]
Dim mass As Double = CDbl([Link])
Dim height As Double = CDbl([Link]) / 100 ' Convert cm to
meters
Dim bmi As Double = mass / (height * height)

Dim result As String


If bmi < 18.5 Then
result = "Underweight"
ElseIf bmi < 24.9 Then
result = "Normal weight"
ElseIf bmi < 29.9 Then
result = "Overweight"
Else
result = "Obesity"
End If

[Link] = "BMI: " & [Link]("0.00") & " - " & result
End Sub
End Class

QUESTION FIVE (20 Marks)

a) Rectangle Area Program (10 Marks)

GUI Design:

 TextBox for length: TextBox1


 TextBox for width: TextBox2
 Button to calculate area: Button1
 Label to display area: Label1

Code:

vb
Copy code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
[Link]
Dim length As Double = CDbl([Link])
Dim width As Double = CDbl([Link])
Dim area As Double = length * width

[Link] = "Area: " & [Link]()


End Sub
End Class

b) Integrated Development Environment (IDE) Components (10 Marks)


i) Solution Explorer:

 Description: Displays the hierarchical view of the projects and files in the solution,
allowing for project and file management.
 Usage: Adding, removing, and organizing files and references within a project.

ii) Toolbox Window:

 Description: Contains a collection of controls and components that can be dragged and
dropped onto the designer surface to build the user interface.
 Usage: Adding buttons, textboxes, labels, etc., to a form.

iii) Properties Window:

 Description: Displays properties of the selected control or component, allowing


developers to set values and configure behavior.
 Usage: Changing the text of a label or setting the background color of a form.

iv) Output Window:

 Description: Displays output messages from the build process, debugging, and other IDE
activities.
 Usage: Viewing build errors, debugging output, and status messages.

v) Menu Bar:

 Description: Provides access to various commands and features of the IDE, such as file
operations, editing, project management, and debugging tools.
 Usage: Opening files, starting debugging sessions, and accessing tools and settings.

4o

You might also like