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

Codes Bible Updated Version April 2021

Visual basic codes and shortcuts for programmers
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)
21 views

Codes Bible Updated Version April 2021

Visual basic codes and shortcuts for programmers
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/ 119

1

Table of Contents
ADDING TWO NUMBERS .................................................................................................................................5

FINANCIAL PROBLEM 1 ....................................................................................................................................6

FINANCIAL PROBLEM 2 ...................................................................................................................................7

FINANCIAL PROBLEM 3 ...................................................................................................................................9

FINANCIAL PROBLEM 4 ................................................................................................................................ 10

MATHEMATICAL PROBLEM 1 ...................................................................................................................... 12

MATHEMATICAL PROBLEM2 ...................................................................................................................... 14

MATHEMATICAL PROBLEM3 ...................................................................................................................... 16

ARRAY PROBLEM 1 .......................................................................................................................................... 18

DASH CIRCLE ..................................................................................................................................................... 19

GENERATE RANDOM NUMBERS ............................................................................................................... 20

SHOP CART......................................................................................................................................................... 21

ONE OF THE INPUTS IS NOT A NUMBER .............................................................................................. 23

CLASS.................................................................................................................................................................... 25

A CLASS WITH MEMBER FUNCTIONS .................................................................................................... 26

PATTERN 1 .......................................................................................................................................................... 27

PATTERN 2 ......................................................................................................................................................... 28

PATTERN 3.......................................................................................................................................................... 29

PATTERN 4 ......................................................................................................................................................... 31

FACTORIAL ......................................................................................................................................................... 33

INHERITANCE .................................................................................................................................................... 34

WRITING AND READING TO A FILE ......................................................................................................... 35

LOGIN APPLICATION THAT AUTHENTICATE USERS ....................................................................... 36

CONSOLE WITH COLORS ............................................................................................................................. 38

STRUCTURE 1 ..................................................................................................................................................... 39

STRUCTURE 2 .................................................................................................................................................... 40

COMPOUND INTEREST ................................................................................................................................. 42

2
FILES AND LIST BOX ...................................................................................................................................... 45

MOON ................................................................................................................................................................... 47

DELIVERY SERVICES ...................................................................................................................................... 48

EMPLOYEE DATABASE.................................................................................................................................. 51

QUADRATIC EQUATION ............................................................................................................................... 54

PROGRESSIVE CLOCK.................................................................................................................................... 56

SIMPLE CALCULATOR .................................................................................................................................... 58

ADVANCED CALCULATOR ........................................................................................................................... 60

ADDING MULTIPLE VALUES IN 0NE TEXTBOX .................................................................................. 65

SCORE BOARD .................................................................................................................................................. 66

SEQUENTIAL SEARCH ALGORITHM ........................................................................................................ 68

PALINDROME .................................................................................................................................................... 71

FILE ON A FORM .............................................................................................................................................. 72

YOU HAVE SELECTED THE CHECKBOX ................................................................................................. 74

SORT TEMPERATURE..................................................................................................................................... 75

ENABLED/DISABLED...................................................................................................................................... 76

AIR ZIMBABWE .................................................................................................................................... 78

FACTORS ............................................................................................................................................................. 80

TENANT DATABASE ....................................................................................................................................... 81

DAY TIME ............................................................................................................................................................ 83

LIBRARY DATABASE....................................................................................................................................... 86

FIBONACCI SERIES ......................................................................................................................................... 89

AMSTRONG NUMBER..................................................................................................................................... 90

CREATING A FOLDER ..................................................................................................................................... 91

INHERITANCE TEST ........................................................................................................................................ 92

FILL COMBO BOX ............................................................................................................................................ 94

CAR RACING ....................................................................................................................................................... 96

TRY CATCH DIVISION BY ZERO ................................................................................................................ 97

3
STRUCTURE WITH FUNCTIONS................................................................................................................. 98

PATTERN 2019 ................................................................................................................................................ 101

STOP WATCH .................................................................................................................................................. 102

DRAW PIE SHAPE .......................................................................................................................................... 104

MATRIX 3X3 ..................................................................................................................................................... 105

UPPER CASE..................................................................................................................................................... 107

FILES AND TEXTBOX ................................................................................................................................... 109

SPIN BUTTON .................................................................................................................................................. 110

COPY CUT & PASTE ...................................................................................................................................... 112

OPEN FILE DIALOG ....................................................................................................................................... 114

SAVE FILE DIALOG ........................................................................................................................................ 115

VALIDATION .................................................................................................................................................... 116

4
ADDING TWO NUMBERS
Module Module1

Sub Main()
Dim num1 As Integer
Dim num2 As Integer
Dim answer As Integer
Console.WriteLine("Enter the first number :")
num1 = Console.ReadLine()
Console.WriteLine("Enter the second number :")
num2 = Console.ReadLine()
answer = num1 + num2
Console.WriteLine("{0} + {1} = {2}", num1, num2, answer)
Console.ReadKey()

End Sub

End Module

5
FINANCIAL PROBLEM 1
Nenyasha works as a part time assistant in a book shop and receives an hourly wage. Enter the number
of hours he worked for a specific week, as well as the pay rate per hour. He has to pay $8 per week
towards the recreation club. Write a console application to calculate and display how much he has
earned for the week.
Module Module1

Sub Main()
Dim hourly_wage As Integer
Dim hours As Integer
Dim result As Integer
Console.WriteLine("Enter hours worked")
hours = Console.ReadLine()
Console.WriteLine("Enter pay rate")
hourly_wage = Console.ReadLine()
result = hourly_wage * hours
Console.WriteLine(" earning for a week is ${0}", result - 8)
Console.ReadKey()
End Sub

End Module

6
FINANCIAL PROBLEM 2

Jane is a typist who works freelance and she charges the following rates:
A page typed single spacing $4.75
A page typed double spacing $3.50
The user must enter the number of pages she typed in single spacing and the number of pages she
typed in double spacing. Calculate the amount due. She saves 15% of this amount to buy supplies.
Write a console application to display the net amount she earned for her work.
Module Module1

Sub Main()
Dim SingleSpacing As Integer
Dim DoubleSpacing As Integer
Dim Cost1 As Single
Dim Cost2 As Single
Dim FinalCost1 As Single
Dim FinalCost2 As Single

Console.WriteLine("Enter the number of pages typed in Single spacing


")
SingleSpacing = Console.ReadLine()
Console.WriteLine("Enter the number of pages typed in Double spacing
")
DoubleSpacing = Console.ReadLine()
Cost1 = SingleSpacing * 4.75
Cost2 = DoubleSpacing * 3.5
FinalCost1 = Cost1 + Cost2
FinalCost2 = 0.15 * FinalCost1
Console.WriteLine("The net amount earned is {0}", FinalCost1 -
FinalCost2)
Console.ReadKey()
End Sub
End Module

7
8
FINANCIAL PROBLEM 3

Everybody in the town Chitungwiza will go to the circus during the weekend. Write a console program
to determine the entrance fee to the circus as persons younger than 12 years or 60 years of age or
more, receive a 25% discount. The user must enter the normal entrance fee and their age in years only.
Display the actual entrance fee to be paid on the screen.
Module Module1

Sub Main()
Dim Age As Integer
Dim EntranceFee As Integer
Dim FinalAmount As Integer
Dim FinalAmount2 As Integer
Console.WriteLine("Enter entrance fee")
EntranceFee = Console.ReadLine()
Console.WriteLine("Enter Age")
Age = Console.ReadLine()
If Age < 12 Or Age >= 60 Then
FinalAmount = 0.25 * EntranceFee
FinalAmount2 = EntranceFee - FinalAmount
Console.WriteLine("Entrance fee is {0}", FinalAmount2)
Else : Console.WriteLine("Entrance fee is {0}", EntranceFee)
End If
Console.ReadKey()
End Sub

End Module

9
FINANCIAL PROBLEM 4
Enock has a gardening service where he and his workers get paid per hour worked as can been seen in the table
below. He also charges an additional $2.50 per hour for equipment used. Write a console program to enter the
hours worked as a real number, calculate and display the amount owed to him. Use a select case statement to
calculate the amount.

Hours worked Payment per hour

0 - 2 $20.00

>2 – 4.5 $19.20

4.6 – less than 6 $18.20

6 and more hours $17.88

Module Module1

Sub Main()
Dim Hours As Integer
Dim Equip As Single
Dim Wage As Single
Console.WriteLine("Enter hours worked")
Hours = Console.ReadLine()

Select Case Hours


Case 0 To 2
Equip = 2.5 * Hours
Wage = 20 * Hours
Console.WriteLine("Amount is {0}", Wage + Equip)

Case 2.1 To 4.5


Equip = 2.5 * Hours
Wage = 19.2 * Hours
Console.WriteLine("Amount is {0}", Wage + Equip)
Case 4.6 To 6
Equip = 2.5 * Hours
Wage = 18.2 * Hours
Console.WriteLine("Amount is {0}", Wage + Equip)

Case Is > 6
Equip = 2.5 * Hours

10
Wage = 17.88 * Hours
Console.WriteLine("Amount is {0}", Wage + Equip)
End Select
Console.ReadKey()

11
MATHEMATICAL PROBLEM 1
Write a VB.NET console program to enter 8 integers between 5 and 48. Display the average of these
numbers.

Module Module1

Sub Main()
Dim I As Integer
Dim Num(8)
Dim Sum As Integer = 0
Dim Ave As Integer

Console.WriteLine("Enter numbers between 5 and 48")


For I = 0 To 7
Num(I) = Console.ReadLine()
If Num(I) < 5 Or Num(I) > 48 Then
Console.WriteLine("Invalid Range")
Else : Sum = Sum + Num(I)
Ave = Sum / 8
End If
Next
Console.WriteLine("The Average is {0}", Ave)

Console.ReadKey()

End Sub

End Module

12
13
MATHEMATICAL PROBLEM2
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim d1 As DateTime = DateTimePicker1.Value
Dim d2 As DateTime = DateTimePicker2.Value
Dim result As TimeSpan = d2.Subtract(d1)
Dim days As Integer = result.TotalDays
Dim Single1 As Decimal = 102.5
Dim Double1 As Decimal = 123.5
Dim Amount As Decimal
If RadioButton1.Checked = True Then
Amount = Single1 * days
End If
If RadioButton2.Checked = True Then
Amount = Double1 * days
End If
Label4.Text = "Number of days: " & days & vbNewLine & "Amount Due: "
& Amount

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
DateTimePicker1.Value = Today
DateTimePicker2.Value = Today
RadioButton1.Checked = False
RadioButton2.Checked = False
Label4.Text = ""
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Me.Close()
End Sub
End Class

14
15
MATHEMATICAL PROBLEM3
Public Class Form1

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Me.Close()
End Sub
Function Hmark(ByVal a As Integer, ByVal b As Integer) As Integer
If a > b Then
Return a
Else
Return b
End If

End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim MajorTest1, MajorTest2, ClassTest1, ClassTest2, ExamMark, max,
Total As Double
MajorTest1 = TextBox1.Text
MajorTest2 = TextBox4.Text
ClassTest1 = TextBox2.Text
ClassTest2 = TextBox5.Text
ExamMark = TextBox3.Text
MajorTest1 = MajorTest1 * 0.15
MajorTest2 = MajorTest2 * 0.2
max = Hmark(ClassTest1, ClassTest2)
max = max * 0.15
ExamMark *= 0.5
Total = MajorTest1 + MajorTest2 + max + ExamMark
If Total > 50 Then
Label7.Text = "The final mark of the student is " & Total & "%"
& vbNewLine & "The result is a Pass"
Else
Label7.Text = "The final mark of the student is " & Total & "%"
& vbNewLine & "The result is a Fail"
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""

16
TextBox4.Text = ""
TextBox5.Text = ""
Label7.Text = ""
End Sub
End Class

17
ARRAY PROBLEM 1
Module Module1

Sub Main()
Dim max, index As Integer
Dim arr() = {4, 15, 38, 53, 8, 95, 129, 653, 45, 658, 12, 40, 0, 98,
74, 32, 9, 39, 25, 87}
max = arr(0)
For i = 0 To 19
If arr(i) > max Then
max = arr(i)
index = i
End If
Next

Console.WriteLine("The highest number is " & max & " Index is " &
index)

Console.ReadKey()
End Sub

End Module

18
DASH CIRCLE
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


PaintEventArgs) Handles MyBase.Paint

Dim pen As New Pen(Me.ForeColor)


pen.Width = 10
pen.DashStyle = Drawing.Drawing2D.DashStyle.Dash
Dim Circle As Rectangle

Circle.X = 35
Circle.Y = 40
Circle.Width = 200
Circle.Height = 200
e.Graphics.DrawEllipse(pen, Circle)
pen.Dispose()

End Sub
End Class

19
GENERATE RANDOM NUMBERS
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim random As New Random()
Dim Numbers(49)
Dim x As Integer

For num = 0 To 49
x = random.Next(20, 200)
ListView1.Items.Add(x)
Numbers(num) = x

Next
Array.Sort(Numbers)
Label1.Text = "Highest number is" & Numbers(49)
End Sub
End Class

20
SHOP CART
Public Class Form1
Private item1, item2, item3, item4, item5, item6 As Decimal
Private shirt As Decimal = 12.0
Private trousers As Decimal = 25.0
Private shoe As Decimal = 55.0
Private socks As Decimal = 3.5
Private hat As Decimal = 5.0
Private suit As Decimal = 95.0

Private Sub Label8_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Label8.Click

End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
item1 = shirt
End Sub

Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
item2 = trousers
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
TextBox1.Text = "$" & item1 + item2 + item3 + item4 + item5 + item6
End Sub

Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
item3 = shoe
End Sub

Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
item4 = socks
End Sub

Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
item5 = hat
End Sub

21
Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
item6 = suit
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
CheckBox1.Checked = False
CheckBox2.Checked = False
CheckBox3.Checked = False
CheckBox4.Checked = False
CheckBox5.Checked = False
CheckBox6.Checked = False
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Application.Exit()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

End Sub
End Class

22
ONE OF THE INPUTS IS NOT A NUMBER
Public Class Form1
Private Number1 As Decimal
Private Number2 As Decimal
Private Answer As Decimal

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Try
Number1 = TextBox1.Text
Number2 = TextBox2.Text
Answer = Number1 / Number2
TextBox3.Text = Answer

Catch

TextBox3.Text = "Error"
Label4.Text = "One of the inputs is not a number. Please try
again!"
End Try
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
Label4.Text = ""

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Me.Close()
End Sub
End Class

23
24
CLASS
Module Module1
Class Box
Public Length As Integer
Public Height As Integer
Public Width As Integer
End Class

Sub Main()
Dim Box1 As Box = New Box()
Dim Box2 As Box = New Box()
Dim Volume As Integer
'Initialising values for Box1
Box1.Length = 6
Box1.Height = 8
Box1.Width = 12
'Initialising values for Box2
Box2.Length = 5
Box2.Height = 9
Box2.Width = 15
Volume = Box1.Length * Box1.Height * Box1.Width
Console.WriteLine("Volume for box1 Is {0}", Volume)
Volume = Box2.Length * Box2.Height * Box2.Width
Console.WriteLine("Volume for box2 Is {0}", Volume)
Console.ReadKey()
End Sub

End Module

25
A CLASS WITH MEMBER FUNCTIONS

Module Module1
Class Box
Public Length As Integer
Public Width As Integer
Public Height As Integer
Public Sub SetLength(ByVal Len As Integer)
Length = Len
End Sub
Public Sub SetWidth(ByVal Wid As Integer)
Width = Wid
End Sub
Public Sub SetHeight(ByVal Hei As Integer)
Height = Hei
End Sub
Public Function GetVolume() As Integer
Return (Length * Height * Width)
End Function
End Class

Sub Main()
Dim Box1 As Box = New Box()
Dim Box2 As Box = New Box()
Dim Volume As Integer

'Specifying Box1
Box1.SetLength(6)
Box1.SetWidth(20)
Box1.SetHeight(12)

'Specifying Box2
Box2.SetLength(30)
Box2.SetWidth(23)
Box2.SetHeight(90)

Volume = Box1.GetVolume()
Console.WriteLine("Volume for Box1 is {0}", Volume)
Volume = Box2.GetVolume()
Console.WriteLine("Volume for Box2 is {0}", Volume)

Console.ReadKey()
End Sub

26
PATTERN 1
Module Module1

Sub Main()
Dim i, j, n As Integer
n = 5
For i = 1 To n
For j = 1 To i
Console.Write(i)
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub

End Module

27
PATTERN 2

Module Module1

Sub Main()
For a = 1 To 7
For b = 1 To 7
If a >= b Then
Console.Write(b)
ElseIf a <= b Then
Console.Write("*")

End If
Next
Console.WriteLine()

Next

Console.ReadKey()
End Sub

End Module

28
PATTERN 3
Module Module1

Sub Main()
Dim n = 2
Dim py = n
Dim px = n
For i = 1 To n
Console.Write(" ")
For j = 1 To n * 2 - 1
If j >= px And j <= py Then
Console.Write("*")
Else : Console.Write(" ")
End If
Next
px = px - 1
py = py + 1
Console.WriteLine(" ")
Console.WriteLine()
Next
n = 3
px = 1
py = n * 2 - 1
For i = 1 To n
For j = 1 To n * 2 - 1
If j >= px And j <= py Then
Console.Write("*")

Else : Console.Write(" ")


End If
Next
px = px + 1
py = py - 1
Console.WriteLine(" ")
Console.WriteLine()
Next

Console.ReadKey()
End Sub

End Module

29
30
PATTERN 4
Module Module1

Sub Main()
Dim n = 7
Dim py = n
Dim px = n
For i = 1 To n

For j = 1 To n * 2 - 1
If j >= px And j <= py Then
Console.Write("*")
Else : Console.Write(" ")
End If
Next
px = px - 1
py = py + 1
Console.WriteLine()
Next
n = 7
px = 1
py = n * 2 - 1
For i = 1 To n
For j = 1 To n * 2 - 1
If j >= px And j <= py Then
Console.Write("*")

Else : Console.Write(" ")


End If
Next
px = px + 1
py = py - 1
Console.WriteLine()
Next

Console.ReadKey()
End Sub

End Module

31
32
FACTORIAL
Public Class Form1
Private Num1 As Decimal
Private Answer As Decimal
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Num1 = TextBox1.Text
Answer = Math.Sqrt(Num1)
MsgBox(Answer)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Num1 = TextBox1.Text
Answer = Num1 ^ (1 / 3)
MsgBox(Answer)

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Dim n, i As Integer
Answer = 1
n = TextBox1.Text
For i = 1 To n
Answer = Answer * i
Next

MsgBox(Answer)
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
TextBox1.Clear()
End Sub
End Class

33
INHERITANCE
Module Module1
Class Shape
Protected Length As Integer
Protected Width As Integer
Public Sub SetLength(ByVal Len As Integer)
Length = Len
End Sub
Public Sub SetWidth(ByVal Wid As Integer)
Width = Wid
End Sub
End Class
'Derived Class
Class Rectangle : Inherits Shape
Public Function GetArea() As Integer
Return (Length * Width)
End Function
End Class
Sub Main()
Dim Rect As Rectangle = New Rectangle()
Rect.SetLength(10)
Rect.SetWidth(5)
Console.WriteLine("Area is {0} :", Rect.GetArea())
Console.ReadKey()
End Sub

End Module

34
WRITING AND READING TO A FILE
Imports System.IO
Module Module1

Sub Main()
Dim s As String
Using sw As StreamWriter = New StreamWriter("D:\names.txt")
Console.WriteLine("Write Somethiung Dude")
s = Console.ReadLine()
sw.WriteLine(s)
End Using
' Read and show each line from the file.
Dim line As String
Using sr As StreamReader = New StreamReader("D:\names.txt")
line = sr.ReadLine()
While (line <> Nothing)
Console.WriteLine(line)
line = sr.ReadLine()
End While
End Using
Console.ReadKey()
End Sub
End Module

35
LOGIN APPLICATION THAT AUTHENTICATE
USERS

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'User2DataSet.tbluser'
table. You can move, or remove it, as needed.
Me.TbluserTableAdapter.Fill(Me.User2DataSet.tbluser)
ListBox1.Visible = False
ListBox2.Visible = False

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Me.Close()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim a As String = ListBox1.FindString(TextBox1.Text)
ListBox1.SelectedIndex = a

36
Dim c As String = ListBox2.FindString(TextBox2.Text)
ListBox2.SelectedIndex = c
If (TextBox1.Text = ListBox1.Text) And (TextBox2.Text =
ListBox2.Text) Then
MsgBox("Welcome", 0 + 64, "Welcome")
Else : MsgBox("access derived", MsgBoxStyle.Critical +
MsgBoxStyle.OkOnly, "Login fails")
End If

End Sub
End Class

37
CONSOLE WITH COLORS

Module Module1

Sub main()

For i = 1 To 4
If i = 1 Then
Console.ForegroundColor = ConsoleColor.White
Console.BackgroundColor = ConsoleColor.Black
Console.WriteLine("I LOVE WORKING WITH COLORS")
ElseIf i = 2 Then
Console.ForegroundColor = ConsoleColor.Red
Console.BackgroundColor = ConsoleColor.Black
Console.WriteLine("I LOVE WORKING WITH COLORS")
ElseIf i = 3 Then
Console.ForegroundColor = ConsoleColor.Red
Console.BackgroundColor = ConsoleColor.White
Console.WriteLine("I LOVE WORKING WITH COLORS")
Else
Console.ForegroundColor = ConsoleColor.Yellow
Console.BackgroundColor = ConsoleColor.Black
Console.WriteLine("I LOVE WORKING WITH COLORS")
End If

Next

Console.ReadKey()
End Sub
End Module

38
STRUCTURE 1

Module Module1
Public Structure City
Public Name As String
Public Population As Integer
Public Diameter As Integer
Public Year As Integer
End Structure
Sub Main()
Dim C As City
Console.WriteLine("Enter City name")
C.Name = Console.ReadLine()
Console.WriteLine("Enter City Population")
C.Population = Console.ReadLine()
Console.WriteLine("Enter City Diameter")
C.Diameter = Console.ReadLine()
Console.WriteLine("Enter City Year")
C.Year = Console.ReadLine()
Console.WriteLine("{0} City had a population of {1} million people
and a diameter of {2}km in the year {3}", C.Name, C.Population, C.Diameter,
C.Year)
Console.ReadKey()
End Sub

End Module

39
STRUCTURE 2
Create an application that makes use of a user defined type to capture student details and display
them. The details to be captured are: Name, course, department, city, phone number.

Module Module1
Structure Student
Dim Name As String
Dim course As String
Dim department As String
Dim city As String
Dim phone_number As String
End Structure
Sub Main()
Dim S(3) As Student
For i = 0 To 2
Console.WriteLine("Student" & i + 1)
Console.WriteLine("Enter Student name")
S(i).Name = Console.ReadLine
Console.WriteLine("Enter Course")
S(i).course = Console.ReadLine
Console.WriteLine("Enter department")
S(i).department = Console.ReadLine
Console.WriteLine("Enter city")
S(i).city = Console.ReadLine
Console.WriteLine("Enter phone number")
S(i).phone_number = Console.ReadLine
Next
For i = 0 To 2
Console.WriteLine("Student" & i + 1)
Console.WriteLine("Student name " & S(i).Name)
Console.WriteLine("Course " & S(i).course)
Console.WriteLine("department " & S(i).department)
Console.WriteLine("city " & S(i).city)

40
Console.WriteLine("phone number " & S(i).phone_number)
Next
Console.ReadKey()
End Sub

End Module

41
COMPOUND INTEREST

Public Class Form1


Private P As Single
Private i As Single
Private n As Single
Private Comp As Integer
Private ri As Double
Private rn As Double
Private Amount As Single

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
P = TextBox1.Text
i = TextBox2.Text
n = TextBox3.Text
ri = i / Comp
rn = n * Comp
Amount = P * (1 + ri / 100) ^ rn
'Compound interest
TextBox4.Text = Amount - P
'Amount Earned
TextBox5.Text = Amount

End Sub

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
Comp = 12
End Sub

42
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
Comp = 3
End Sub

Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
Comp = 2
End Sub

Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
Comp = 1
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Me.Close()
End Sub
End Class

43
44
FILES AND LIST BOX
Using VB.Net design and write code for an application to create the file sports.txt on any location of
the computer, with the following details: Football, Basketball, Golf and Volleyball. Display the content
of the file sports.txt in a Listbox.
Imports System.IO
Public Class Form1

Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnCreate.Click
Dim sports As String() = New String() {"Football", "Basketball",
"Golf", "Volleyball"}
Dim s As String
Using sw As StreamWriter = New StreamWriter("sports.txt")
For Each s In sports
sw.WriteLine(s)
Next s
End Using
' Read and show each line from the file.
Dim line As String
Using sr As StreamReader = New StreamReader("sports.txt")
line = sr.ReadLine()
While (line <> Nothing)
lstSports.Items.Add(line)
line = sr.ReadLine()
End While
End Using
End Sub
End Class

45
46
MOON
Imports System.Drawing.Drawing2D
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
BackColor = Color.Black
e.Graphics.FillEllipse(Brushes.White, 50, 30, 210, 210)
e.Graphics.FillEllipse(Brushes.Black, 90, 30, 210, 210)
End Sub
End Class

47
DELIVERY SERVICES

Public Class Form1

Function travelling_way() As Decimal


Dim Tcost As Decimal
If rbtRoad.Checked = True Then
Tcost = 0.15

ElseIf rbtRailway.Checked = True Then


Tcost = 0.12

ElseIf rbtAir.Checked = True Then


Tcost = 0.12
ElseIf rbtBoat.Checked = True Then
Tcost = 0.25
End If
Return Tcost
End Function
Function Basic_cost() As Decimal
lblBasic_Cost.Text = (1.23 * Val(txtWeight.Text)) *
Val(txtDistance.Text) * travelling_way()
Return lblBasic_Cost.Text
End Function
Public Sub Extra()

48
If chkInsurance.Checked = True Then
lblInsurance_Cost.Text = 0.11 * Val(lblBasic_Cost.Text)

End If
If chkPriority.Checked = True Then
lblPriority_Cost.Text = 0.15 * Val(lblBasic_Cost.Text)
End If

End Sub
Function SubTotal() As Decimal
lblSub_Total.Text = Basic_cost() + Val(lblInsurance_Cost.Text) +
Val(lblPriority_Cost.Text)
Return lblSub_Total.Text
End Function
Sub Display()
travelling_way()
Basic_cost()
Extra()
lblVat.Text = 0.14 * SubTotal()
txtTotalCost.Text = Val(lblVat.Text) + SubTotal()

End Sub

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnCalculate.Click

Display()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnClear.Click
txtDistance.Text = ""
txtTotalCost.Text = ""
txtWeight.Text = ""
lblBasic_Cost.Text = ""
lblInsurance_Cost.Text = ""
lblPriority_Cost.Text = ""

49
lblSub_Total.Text = ""
lblVat.Text = ""
rbtAir.Checked = False
rbtBoat.Checked = False
rbtRailway.Checked = False
rbtRoad.Checked = False
chkInsurance.Checked = False
chkPriority.Checked = False
txtWeight.Focus()
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class

50
EMPLOYEE DATABASE

51
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'StudentsDataSet.Stud'
table. You can move, or remove it, as needed.
Me.StudTableAdapter.Fill(Me.StudentsDataSet.Stud)
'TODO: This line of code loads data into the 'StudentsDataSet.Stud'
table. You can move, or remove it, as needed.
Me.StudTableAdapter.Fill(Me.StudentsDataSet.Stud)
'TODO: This line of code loads data into the 'StudentsDataSet.Stud'
table. You can move, or remove it, as needed.
Me.StudTableAdapter.Fill(Me.StudentsDataSet.Stud)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
StudBindingSource.AddNew()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
StudBindingSource.EndEdit()
StudTableAdapter.Update(StudentsDataSet.Stud)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
StudBindingSource.RemoveCurrent()
End Sub

Private Sub BindingNavigatorMoveNextItem_Click(ByVal sender As


System.Object, ByVal e As System.EventArgs)

End Sub
End Class

52
53
QUADRATIC EQUATION

Public Class Form1


Private a, b, c, det As Integer
Private root1, root2 As Single

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click

a = TextBox1.Text
b = TextBox2.Text
c = TextBox3.Text
det = (b ^ 2) - (4 * a * c)
If (b ^ 2) > (4 * a * c) Then
TextBox4.Text = "2"
root1 = (-b + Math.Sqrt(det)) / (2 * a)
root2 = (-b - Math.Sqrt(det)) / (2 * a)
TextBox5.Text = root1
TextBox6.Text = root2
ElseIf (b ^ 2) < (4 * a * c) Then
MsgBox("No roots found", 0 + 16, "Error Report")
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""

54
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Me.Close()
End Sub

Private Sub Button1_DoubleClick(ByVal sender As Object, ByVal e As


System.EventArgs) Handles Button1.DoubleClick

End Sub
End Class

55
PROGRESSIVE CLOCK

Public Class Form1


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Timer1.Start()

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Timer1.Tick
Label6.Text = Date.Now.Hour.ToString
Label7.Text = Date.Now.Minute.ToString
Label8.Text = Date.Now.Second.ToString
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = 24
ProgressBar2.Minimum = 0
ProgressBar2.Maximum = 60
ProgressBar3.Minimum = 0
ProgressBar3.Maximum = 60
ProgressBar1.Value = Date.Now.Hour.ToString
ProgressBar2.Value = Date.Now.Minute.ToString
ProgressBar3.Value = Date.Now.Second.ToString

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
End Class

56
57
SIMPLE CALCULATOR
Public Class Form1
Private num1 As Integer
Private num2 As Integer
Private result As Integer

Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
result = num1 / num2
TextBox3.Text = result
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

End Sub

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
num1 = Val(TextBox1.Text)
num2 = Val(TextBox2.Text)
result = num1 + num2
TextBox3.Text = result
End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
num1 = Val(TextBox1.Text)
num2 = Val(TextBox2.Text)
result = num1 - num2
TextBox3.Text = result
End Sub

Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
num1 = Val(TextBox1.Text)
num2 = Val(TextBox2.Text)
result = num1 * num2
TextBox3.Text = result
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click

58
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class

59
ADVANCED CALCULATOR
'================================CALCULATOR CODE==========================================

Private Sub Button22_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button22.Click

If Label22.Text = "0" Then


Label22.Text = "0"
Else
Label22.Text = Label22.Text + "0"
End If
End Sub

Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button20.Click

If Label22.Text = "0" Then


Label22.Text = "1"
Else
Label22.Text = Label22.Text + "1"
End If

End Sub

Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button19.Click
If Label22.Text = "0" Then
Label22.Text = "2"
Else
Label22.Text = Label22.Text + "2"
End If

End Sub

Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button18.Click
If Label22.Text = "0" Then
Label22.Text = "3"
Else
Label22.Text = Label22.Text + "3"
End If

End Sub

60
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button16.Click
If Label22.Text = "0" Then
Label22.Text = "4"
Else
Label22.Text = Label22.Text + "4"
End If

End Sub

Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button15.Click
If Label22.Text = "0" Then
Label22.Text = "5"
Else
Label22.Text = Label22.Text + "5"
End If
End Sub

Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button14.Click
If Label22.Text = "0" Then
Label22.Text = "6"
Else
Label22.Text = Label22.Text + "6"
End If

End Sub

Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button12.Click
If Label22.Text = "0" Then
Label22.Text = "7"
Else
Label22.Text = Label22.Text + "7"
End If

End Sub

Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button11.Click
If Label22.Text = "0" Then
Label22.Text = "8"
Else
Label22.Text = Label22.Text + "8"
End If

61
End Sub

Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button10.Click
If Label22.Text = "0" Then
Label22.Text = "9"
Else
Label22.Text = Label22.Text + "9"
End If

End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button8.Click
If Not (Label22.Text.Contains(".")) Then
Label22.Text += "."
End If
End Sub

Private Sub Button21_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button21.Click
firstvalue = Label22.Text
Label20.Text = firstvalue & "+"
Label22.Text = ""
operation = "+"
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button6.Click
Label22.Text = "0"
Label20.Text = "0"
End Sub

Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button17.Click
firstvalue = Label22.Text
Label20.Text = firstvalue & "-"
Label22.Text = ""
operation = "-"
End Sub

Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button13.Click
firstvalue = Label22.Text
Label20.Text = firstvalue & "*"

62
Label22.Text = ""
operation = "*"
End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button9.Click
firstvalue = Label22.Text
Label20.Text = firstvalue & "/"
Label22.Text = ""
operation = "/"
End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button7.Click
firstvalue = Label22.Text
Label20.Text = firstvalue & "^"
Label22.Text = ""
operation = "^"
End Sub

Private Sub Button23_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button23.Click

secondvalue = Label22.Text
If operation = "+" Then
answer = firstvalue + secondvalue
Label22.Text = answer
Label20.Text = ""
ElseIf operation = "^" Then
answer = firstvalue ^ secondvalue
Label22.Text = answer
Label20.Text = ""

ElseIf operation = "-" Then


answer = firstvalue - secondvalue
Label22.Text = answer
Label20.Text = ""
ElseIf operation = "-" Then
answer = firstvalue - secondvalue
Label22.Text = answer
Label20.Text = ""
ElseIf operation = "*" Then
answer = firstvalue * secondvalue
Label22.Text = answer
Label20.Text = ""
ElseIf operation = "/" And secondvalue <> 0 Then

63
answer = firstvalue / secondvalue
Label22.Text = "0"
Label22.Text = answer
Label20.Text = ""
Else : MsgBox("Hazviite zvaurikuda kuita", MsgBoxStyle.Critical +
MsgBoxStyle.OkOnly, "Error")
Label22.Text = "0"
Label20.Text = "0"
End If

If Label22.Text.Length > 15 Then


MsgBox("Manhamba awandisa", MsgBoxStyle.Critical +
MsgBoxStyle.OkOnly, "Error")
Label22.Text = "0"
End If
End Sub

64
ADDING MULTIPLE VALUES IN 0NE TEXTBOX
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Num As String() = TextBox1.Text.Split(" ")
Dim Total As Long
For Each item As String In Num
Total += CType(item, Long)

Next
MsgBox(Total)

End Sub
End Class

65
SCORE BOARD

Public Class Form1


Private a As Integer = 0
Private b As Integer = 0
Private c As Integer = 0
Private Name1 As String
Private Name2 As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Name1 = TextBox1.Text
Name2 = TextBox4.Text

a = a + 1
TextBox2.Text = a
If a > b Then
TextBox5.Text = Name1 + " lead!"
ElseIf a < b Then
TextBox5.Text = Name2 + " lead!"
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Name1 = TextBox1.Text
Name2 = TextBox4.Text

b = b + 1
TextBox3.Text = b
If a > b Then
TextBox5.Text = Name1 + " lead!"
ElseIf a < b Then
TextBox5.Text = Name2 + " lead!"
End If
End Sub

66
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
c = c + 1
TextBox6.Text = c
If c > 2 Then
c = 1
TextBox6.Text = c
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
Me.Close()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
TextBox2.Text = "0"
TextBox3.Text = "0"
End Sub
End Class

67
SEQUENTIAL SEARCH ALGORITHM
Module Module1
Function sequSearch(ByVal dataset() As Integer, ByVal target As Integer,
ByVal n As Integer) As Integer
Dim i As Integer
Dim pos As Integer = -1
Dim found As Boolean = False
For i = 0 To n - 1 And found <> True
If (target = dataset(i)) Then
pos = i
found = True
End If
Next
Return pos
End Function
Sub Main()
Dim arr() As Integer = New Integer() {3, 12, 32, 34, 45, 90, 4, 20,
52, 74}
Dim pos As Integer
Dim tar As Integer
Console.Write("Find:")
tar = Integer.Parse(Console.ReadLine())
pos = sequSearch(arr, tar, arr.Length)
If (pos <> -1) Then
Console.WriteLine("Found at {0}", pos)

Else : Console.WriteLine("Not found!")


End If
Console.ReadKey()
End Sub

End Module

68
RANDOM NUMBERS

Public Class Form1


Dim Random As New Random
Dim Random2 As New Random
Dim myRand As Integer
Dim myRand2 As Integer
Dim sum As Integer

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles TextBox1.TextChanged

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
Label1.Text = 0
Label3.Text = 0
End Sub

69
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Me.Close()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
myRand = Random.Next(0, 100)
Label1.Text = myRand
myRand2 = Random2.Next(0, 100)
Label3.Text = myRand2.ToString

End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

sum = myRand + myRand2


If TextBox1.Text = sum Then
MsgBox("Waal that's excellent", MsgBoxStyle.Information +
MsgBoxStyle.OkOnly, "Message")
Else
MsgBox("Oh no try again", MsgBoxStyle.Critical +
MsgBoxStyle.OkOnly, "Message")
End If
End Sub
End Class

70
PALINDROME
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim Name As String = TextBox1.Text
If Name = StrReverse(Name) Then
MsgBox("Is A Palindrome")

Else
MsgBox("Is not a palindrome")
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
End
End Sub
End Class

71
FILE ON A FORM

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim Employee As String() = New String() {TextBox2.Text,
TextBox3.Text, ComboBox1.Text, TextBox4.Text, TextBox1.Text}

Dim s As String
Try
Using sw As StreamWriter = New StreamWriter(TextBox5.Text)
For Each s In Employee
sw.WriteLine(s)
Next s
End Using
MsgBox("Saved!", 1 + 64)
Catch

MsgBox("Not Saved!", 0 + 16)


End Try
End Sub

72
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

Dim line As String


Try
Using sr As StreamReader = New StreamReader(TextBox6.Text)
line = sr.ReadLine()
While (line <> Nothing)
MsgBox(line)
line = sr.ReadLine()
End While
End Using
Catch
MsgBox("File not found!", 1 + 16)
End Try
End Sub
End Class

73
YOU HAVE SELECTED THE CHECKBOX

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
If CheckBox1.Checked = True Then
Label1.Text = "You have selected the checkbox"
Else
Label1.Text = "You have not selected the checkbox"
End If
End Sub
End Class

74
SORT TEMPERATURE

Module Module1

Sub Main()
Dim Temp() = {34, 21, 19, 33, 41, 49, 13, 9, 56, 30}
Dim i As Integer
Array.Sort(Temp)
Console.WriteLine("Temperature starting with the hotest city")
For Each i In Temp
Console.WriteLine(i)
Next
Console.ReadKey()
End Sub

End Module

75
ENABLED/DISABLED

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
TextBox1.Enabled = True
TextBox2.Enabled = True
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
TextBox1.Enabled = False
TextBox2.Enabled = False
End Sub
End Class

76
77
AIR ZIMBABWE

Module Module1

Sub Main()
Dim kg As Integer
Dim cost As Decimal
Dim cost2 As Decimal
Dim FinalCost As Decimal
Console.WriteLine("Enter kgs")
kg = Console.ReadLine
If kg <= 30 Then
cost = kg * 2
Console.WriteLine("The Charge is " & cost)
ElseIf kg > 30 And kg <= 40 Then
cost = 30 * 2
cost2 = (kg - 31) * 3.5
FinalCost = cost + cost2
Console.WriteLine("The charge is " & FinalCost)
ElseIf kg > 40 And kg <= 50 Then
cost = (30 * 2) + (9 * 3.5)

78
cost2 = (kg - 41) * 5.5
FinalCost = cost + cost2
Console.WriteLine("The charge is " & FinalCost)
Else
Console.WriteLine("Please put your luggage on a cargo")
End If
Console.ReadKey()
End Sub

End Module

79
FACTORS

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
For i = 2 To 100
If 100 Mod i = 0 Then
ListBox1.Items.Add(i)
End If
Next
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

End Sub
End Class

80
TENANT DATABASE

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the
'TenantsDataSet.tbltenants' table. You can move, or remove it, as needed.
Me.TbltenantsTableAdapter.Fill(Me.TenantsDataSet.tbltenants)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click

TbltenantsBindingSource.EndEdit()
TbltenantsTableAdapter.Update(TenantsDataSet.tbltenants)
TbltenantsBindingSource.AddNew()

End Sub

Private Sub SearchToolStripButton_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles SearchToolStripButton.Click
Try
Me.TbltenantsTableAdapter.Search(Me.TenantsDataSet.tbltenants,
Tenant_NameToolStripTextBox.Text)

81
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
TbltenantsTableAdapter.Search(TenantsDataSet.tbltenants,
TextBox4.Text)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
TbltenantsBindingSource.RemoveCurrent()
End Sub
End Class

(Tenant Name LIKE?+’%’)

82
DAY TIME

Public Class Form1


Private hour As Integer
Private minute As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
hour = InputBox("Please Enter hour")
minute = InputBox("Please Enter minute")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
If hour >= 21 And hour <= 23 And minute >= 0 And minute <= 59 Then
lblHour.Text = hour & ":" & minute
lblTime.Text = "Night"
lblColor.Text = "Black"

ElseIf hour >= 0 And hour <= 5 And minute >= 0 And minute <= 59 Then
lblHour.Text = hour & ":" & minute
lblTime.Text = "Night"

83
lblColor.Text = "Black"

ElseIf hour >= 6 And hour <= 10 And minute >= 0 And minute <= 59
Then
lblHour.Text = hour & ":" & minute
lblTime.Text = "Morning"
lblColor.Text = "Yellow"
ElseIf hour >= 11 And hour <= 12 And minute >= 0 And minute <= 59
Then
lblHour.Text = hour & ":" & minute
lblTime.Text = "Lunch"
lblColor.Text = "Orange"
ElseIf hour >= 14 And hour <= 17 And minute >= 0 And minute <= 59
Then
lblHour.Text = hour & ":" & minute
lblTime.Text = "Afternoon"
lblColor.Text = "Light Blue"
ElseIf hour >= 18 And hour <= 20 And minute >= 0 And minute <= 59
Then
lblHour.Text = hour & ":" & minute
lblTime.Text = "Evening"
lblColor.Text = "Dark Blue"
Else
lblHour.Text = "Invalid Input"
lblTime.Text = "Invalid Input"
lblColor.Text = "Invalid Input"
End If
End Sub
End Class

84
85
LIBRARY DATABASE
Create a database in MS-Access, call it Library and put the following fields:

Book title, Author, Publisher, Year, Category.

Write a program and link it to the database. Your interface should be like the one below.

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

'TODO: This line of code loads data into the 'LibraryDataSet.Table1'


table. You can move, or remove it, as needed.
Me.Table1TableAdapter.Fill(Me.LibraryDataSet.Table1)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Try
Table1BindingSource.EndEdit()
Table1TableAdapter.Update(LibraryDataSet.Table1)
MsgBox("Saved")

86
Catch
MsgBox("Not Saved")
End Try
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Table1BindingSource.AddNew()

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
Table1BindingSource.RemoveCurrent()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Table1BindingSource.MoveNext()
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button6.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button5.Click
End
End Sub
End Class

87
88
FIBONACCI SERIES
Write a program that allows the user to enter a number into a text box. Your program should then
calculate and display the first n terms of the Fibonacci series. The terms are displayed in a list box
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim n As Integer
Dim a As Integer = 0
Dim b As Integer = 1
n = TextBox1.Text
For i As Integer = 0 To n - 1
Dim temp As Integer = a
a = b
b = temp + b
ListBox1.Items.Add(a)
Next
End Sub
End Class

89
AMSTRONG NUMBER
Module Module1

Sub Main()
Dim r As Integer
Dim number As Long = 0
Dim sum As Long = 0
Dim c, temp As Long
Console.WriteLine("Enter an Integer up to which you want to find
amstrong number")
number = Console.ReadLine()
Console.WriteLine("Following Amstrong number are found from 1 To
{0}", number)
For c = 1 To number Step 1
temp = c
While temp <> 0
r = temp Mod 10
sum = sum + r * r * r
temp = temp / 10
End While
If c = sum Then
Console.WriteLine(c)
sum = 0
End If
Next
Console.ReadKey()
End Sub

End Module

90
CREATING A FOLDER

Imports System.IO
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim d1 As DirectoryInfo = New DirectoryInfo(TextBox1.Text)
If d1.Exists Then
MsgBox("That path exists already.")
TextBox1.Clear()
Else
d1.Create()
MsgBox("Folder is created....!", 0 + 64)
TextBox1.Clear()

End If
End Sub
End Class

91
INHERITANCE TEST

92
Public Class Form1
Class Person
Public Name As String
Public Address As String
Public Sub Enroll()
MsgBox("True-Called from Person")

End Sub
End Class
Class Student : Inherits Person
Dim S As Person = New Person
Sub Enrol()
MsgBox("True-Called from Student")
End Sub
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim S As Person = New Person
Dim S1 As Student = New Student

S.Enroll()
S1.Enrol()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

End Sub
End Class

93
FILL COMBO BOX

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
ListBox1.Items.Add(ComboBox1.SelectedItem)

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
ComboBox1.Items.Add("Vb.Net")
ComboBox1.Items.Add("Software Engineering")
ComboBox1.Items.Add("COMPARCH")
ComboBox1.Items.Add("Data Communication")
ComboBox1.Items.Add("IT Research")
ComboBox1.Items.Add("Maths")

End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button5.Click
ListBox1.Sorted = True
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
ListBox1.Items.Clear()
End Sub
End Class

94
95
CAR RACING
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Timer1.Start()

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Timer1.Tick

Label1.Location = New Point(Label1.Right + 1, 42)


Label2.Location = New Point(Label1.Right + 1, 113)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Timer1.Stop()
End Sub

End Class

96
TRY CATCH DIVISION BY ZERO

Module Module1

Sub Main()
Dim num1 As Integer
Dim num2 As Integer
Dim Answer As Decimal
num1 = InputBox("Input num1")
num2 = InputBox("Input num2")
Try
Answer = num1 / num2
MsgBox("Your answer is " & Answer)
Catch ex As Exception
MsgBox("You cannot divide by zero", 1 + 16, "Error")
End Try

End Sub

End Module

97
STRUCTURE WITH FUNCTIONS

98
Module Module1
Structure Customer
Dim Name As String
Dim Income As Integer
Dim Delivery_Zone As Integer
Public Function Delivery_Cost() As Decimal
Dim Cost As Decimal
If Delivery_Zone = 1 Then
Cost = 10.0
ElseIf Delivery_Zone = 2 Then
Cost = 20.0
ElseIf Delivery_Zone = 3 Then
Cost = 30.0
ElseIf Delivery_Zone = 4 Then
Cost = 40.0
End If
Return Cost
End Function
End Structure
Sub Main()
Dim C(4) As Customer
For i = 0 To 4
Console.WriteLine("Customer {0}", i + 1)
Console.WriteLine("Enter customer name")
C(i).Name = Console.ReadLine()
Console.WriteLine("Enter Income")
C(i).Income = Console.ReadLine()
Console.WriteLine("Enter Delivery zone")
C(i).Delivery_Zone = Console.ReadLine()
Next
Console.WriteLine("Name" & vbTab & vbTab & "Income $" & vbTab &
"Zone" & vbTab & "Cost $")
For i = 0 To 4
Console.WriteLine(C(i).Name & vbTab & vbTab & C(i).Income &
vbTab & vbTab & C(i).Delivery_Zone & vbTab & C(i).Delivery_Cost())

Next

Console.ReadKey()
End Sub

End Module

99
100
PATTERN 2019

Module Module1

Sub Main()
For a = 0 To 5
For b = 0 To 5
If a Mod 2 = 0 Then
Console.Write(" * ")
Else
Console.Write(" *")
End If
Next
Console.WriteLine()
Next
Console.ReadKey()
End Sub

End Module

101
STOPWATCH

Public Class Form1


Private a As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Timer1.Tick
Label1.Text = a
a += 1
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Timer1.Stop()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
a = 0

102
End Sub

End Class

103
DRAW PIE SHAPE

Imports System.Drawing.Drawing2D
Public Class Form1

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
BackColor = Color.Black
e.Graphics.FillEllipse(Brushes.White, 50, 30, 210, 210)
e.Graphics.FillRectangle(Brushes.Black, 145, 25, 119, 115)
End Sub

End Class

104
MATRIX 3X3

Module Module1

Sub Main()
Dim matrix1(3, 3) As Integer
Dim matrix2(3, 3) As Integer

Dim Product As Integer = 0


Dim Scalar As Integer
Console.WriteLine("Enter the Scalar")
Scalar = Console.ReadLine
Console.WriteLine("Enter Matrix1: ")
For i = 0 To 2
For j = 0 To 2

Console.Write("Enter element[{0}][{1}]: ", i, j)


matrix1(i, j) = Console.ReadLine
Next
Next

'Multiplication of two matrices


For i = 0 To 2
For j = 0 To 2

Product = 0
For k = 0 To 2
Product = Scalar * (matrix1(i, j))
Next
matrix2(i, j) = Product
Next
Next

Console.WriteLine("Matrix1: ")
For i = 0 To 2
For j = 0 To 2

105
Console.Write("{0} ", matrix1(i, j))
Next
Console.WriteLine()
Next

Console.WriteLine("Matrix2: ")
For i = 0 To 2
For j = 0 To 2

Console.Write("{0} ", matrix2(i, j))


Next
Console.WriteLine()
Next

Console.WriteLine("Multiplication of Matrix1 : ")


For i = 0 To 2
For j = 0 To 2

Console.Write("{0} ", matrix2(i, j))


Next
Console.ReadKey()
Next

End Sub

End Module

106
UPPER CASE

Public Class Form1


Private Word As String
Private Formatted As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Word = TextBox1.Text
Formatted = StrConv(TextBox1.Text, vbProperCase)
TextBox2.Text = Formatted
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Word = TextBox1.Text
Formatted = Word.ToUpper()
TextBox2.Text = Formatted
End Sub

End Class

107
108
FILES AND TEXTBOX

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
FileOpen(1, "tanaka.txt", OpenMode.Output)
PrintLine(1, TextBox1.Text)

FileClose()

FileOpen(1, "tanaka.txt", OpenMode.Input)


While Not EOF(1)
TextBox2.Text = LineInput(1)
End While

End Sub
End Class

109
SPIN BUTTON

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs)

End Sub

Private Sub DomainUpDown1_SelectedItemChanged(ByVal sender As


System.Object, ByVal e As System.EventArgs) Handles
DomainUpDown1.SelectedItemChanged
Dim a, b, c As Integer
If DomainUpDown1.Text = "Random" Then
Dim Random As New Random

a = Random.Next(0, 9)
Label1.Text = a
b = Random.Next(0, 9)
Label2.Text = b
c = Random.Next(0, 9)
Label3.Text = c
End If
If a = 7 Then
PictureBox1.Visible = True
End If
If b = 7 Then
PictureBox1.Visible = True
End If
If c = 7 Then
PictureBox1.Visible = True
End If

If DomainUpDown1.Text = "Hide" Then


PictureBox1.Visible = False

110
End If
If DomainUpDown1.Text = "Random" Then

End If
End Sub

End Class

111
COPY, CUT & PASTE

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
RichTextBox1.Cut()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
RichTextBox1.Copy()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
RichTextBox1.Paste()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

End Sub
End Class

112
113
OPEN FILE DIALOG

Imports System.IO
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
' Call ShowDialog.
Dim result As DialogResult = OpenFileDialog1.ShowDialog()
' Test result.
If result = Windows.Forms.DialogResult.OK Then
' Get the file name.
Dim path As String = OpenFileDialog1.FileName
Try
' Read in text.
Dim text As String = File.ReadAllText(path)
' For debugging.
Me.Text = text.Length.ToString
Catch ex As Exception
' Report an error.
Me.Text = "Error"
End Try
End If
End Sub
End Class

114
SAVE FILE DIALOG

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt"
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK _
Then
My.Computer.FileSystem.WriteAllText _
(SaveFileDialog1.FileName, RichTextBox1.Text, True)
End If
End Sub

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

End Sub

End Class

115
VALIDATION

116
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
MsgBox("Please Enter Title.Mandatory", MessageBoxButtons.OK)
End If
If TextBox2.Text = "" Then
MsgBox("Please Enter Last Name.Mandatory", MessageBoxButtons.OK)
End If
If TextBox3.Text = "" Then
MsgBox("Please First Name .Mandatory", MessageBoxButtons.OK)
End If
If TextBox4.Text = "" Then
MsgBox("Please Enter Postal Address.Mandatory",
MessageBoxButtons.OK)
End If
If TextBox5.Text = "" Then
MsgBox("Please Enter Country.Mandatory", MessageBoxButtons.OK)
End If
End Sub

End Class

117
118
TANLEY INCOPERATED © 2021 ALL RIGHTS RESERVED

For more information get in touch:

+263734246006

[email protected]

119

You might also like