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

Lab 4 Report

The document describes 6 steps to write Visual Basic and C# programs: 1. Create a program to display the alphabet on buttons and output the letter clicked. 2. Modify the program to arrange buttons in rows with different colors. 3. Write a console program to get 3 numbers from the user, find the largest, smallest, and average. 4. Create a Windows form to get 3 numbers in textboxes and display the results. 5. Write a console program to get marks for 10 students and find the highest, lowest, and average marks. 6. Expand the program to get marks for 2 classes and determine the highest average class and best/worst students overall.

Uploaded by

Wei Chun Ang
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)
34 views

Lab 4 Report

The document describes 6 steps to write Visual Basic and C# programs: 1. Create a program to display the alphabet on buttons and output the letter clicked. 2. Modify the program to arrange buttons in rows with different colors. 3. Write a console program to get 3 numbers from the user, find the largest, smallest, and average. 4. Create a Windows form to get 3 numbers in textboxes and display the results. 5. Write a console program to get marks for 10 students and find the highest, lowest, and average marks. 6. Expand the program to get marks for 2 classes and determine the highest average class and best/worst students overall.

Uploaded by

Wei Chun Ang
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
You are on page 1/ 12

Practical Work 2: Visual Basic Fundamentals:

Step1: Construct a Windows application program to display the 26


alphabet on array of control buttons.
Coding Form:
Public Class alphabetForm
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim button(26) As Button
Dim i As Integer
Dim n As Integer
Dim myfont As New Font("Times New Roman", 12, FontStyle.Italic)
For i = 0 To 25
button(i) = New Button()
button(i).Width = 30
button(i).Height = 30
button(i).Top = 50
button(i).BackColor = Color.BlueViolet
button(i).ForeColor = Color.White
button(i).Font = myfont
button(i).TextAlign = ContentAlignment.MiddleCenter
button(i).Left = i * 30
Me.Controls.Add(button(i))
button(i).Text = Chr(n + 65)
n += 1
AddHandler button(i).Click, AddressOf button_click
button(i).BringToFront()
Next
Me.BackColor = Color.Aquamarine
End Sub
Private Sub button_click(ByVal sender As Object, ByVal e As EventArgs)
txtoutput.Text = sender.text
End Sub
End Class

Design Form:

Output:

Step 2:Modify the program in Step 1 to produce an output as


below:
Design Form:

Coding Form:
Public Class azform
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim button(26) As Button
Dim i As Integer
Dim n As Integer
Dim myfont As New Font("New Times Roman", 12, FontStyle.Italic)
Dim buttonA As Button
Dim buttonB As Button
For i = 0 To 6
button(i) = New Button()
button(i).Width = 30
button(i).Height = 30
button(i).Top = 50
button(i).BackColor = Color.DarkOliveGreen
button(i).ForeColor = Color.White
button(i).Font = myfont
button(i).Left = (i * 30) + 15
Me.Controls.Add(button(i))
button(i).Text = Chr(n + 65)
n += 1
AddHandler button(i).Click, AddressOf button_click
button(i).BringToFront()
Next
For i = 7 To 13
button(i) = New Button()
button(i).Width = 30
button(i).Height = 30
button(i).Top = 90
button(i).BackColor = Color.SkyBlue
button(i).Font = myfont
button(i).Left = ((i - 7) * 30) + 15
Me.Controls.Add(button(i))
button(i).Text = Chr(n + 65)
n += 1
AddHandler button(i).Click, AddressOf button_click
button(i).BringToFront()
Next
For i = 14 To 20
button(i) = New Button()
button(i).Width = 30
button(i).Height = 30

button(i).Top = 130
button(i).BackColor = Color.PaleVioletRed
button(i).Font = myfont
button(i).Left = ((i - 14) * 30) + 15
Me.Controls.Add(button(i))
button(i).Text = Chr(n + 65)
n += 1
AddHandler button(i).Click, AddressOf button_click
button(i).BringToFront()
Next
For i = 21 To 25
button(i) = New Button()
button(i).Width = 30
button(i).Height = 30
button(i).Top = 170
button(i).BackColor = Color.BlueViolet
button(i).Font = myfont
button(i).Left = ((i - 21) * 30) + 15
Me.Controls.Add(button(i))
button(i).Text = Chr(n + 65)
n += 1
AddHandler button(i).Click, AddressOf button_click
button(i).BringToFront()
Next
buttonA = New Button()
buttonA.Width = 30
buttonA.Height = 30
buttonA.Top = 170
buttonA.BackColor = Color.BlueViolet
buttonA.Font = myfont
buttonA.Left = ((26 - 21) * 30) + 15
Me.Controls.Add(buttonA)
buttonA.Text = ("@")
AddHandler buttonA.Click, AddressOf button_click
buttonA.BringToFront()
buttonB = New Button()
buttonB.Width = 30
buttonB.Height = 30
buttonB.Top = 170
buttonB.BackColor = Color.BlueViolet
buttonB.Font = myfont
buttonB.Left = ((27 - 21) * 30) + 15
Me.Controls.Add(buttonB)
buttonB.Text = ("#")
AddHandler buttonB.Click, AddressOf button_click
buttonB.BringToFront()
Me.BackColor = Color.Aquamarine
End Sub
Private Sub button_click(ByVal sender As Object, ByVal e As EventArgs)
txtoutput.Text = sender.text
End Sub
End Class

Output:

Display A when clicked A.


Step 3: write a console application program that asks the user
to input 3 integer and compare these number to find largest,
smallest and the average then display it.
Coding Form:
Module Module1
Sub Main()
Dim value(3) As Integer
'value(3) =array
Dim i As Integer
Dim counter As Integer
' declaration value(),counter,i(as sum),average
Dim average As Double
For counter = 0 To 2
Console.Write("Insert your number(" & counter + 1 & "):") 'user insert input number
value(counter) = Console.ReadLine()
i += value(counter)
average = i / 3
Next
Console.WriteLine()
If value(0) > value(1) And value(0) > value(2) Then
' for largest number
Console.WriteLine("The largest number is " & value(0) & ".")
ElseIf value(1) > value(0) And value(1) > value(2) Then
Console.WriteLine("The largest number is " & value(1) & ".")
ElseIf value(2) > value(0) And value(2) > value(1) Then
Console.WriteLine("The largest number is " & value(2) & ".")
Else
Console.WriteLine("Please Try Again.")
End If
If value(0) < value(1) And value(0) < value(2) Then
' for smallest number
Console.WriteLine("The smallest number is " & value(0) & ".")
ElseIf value(1) < value(0) And value(1) < value(2) Then
Console.WriteLine("The smallest number is " & value(1) & ".")
ElseIf value(2) < value(0) And value(2) < value(1) Then
Console.WriteLine("The smallest number is " & value(2) & ".")
Else
Console.WriteLine("Please Try Again.")
End If
Console.WriteLine("The average of 3 number is:" & Format(average, "0.00") & ".")
Console.ReadLine()
End Sub
End Module

Output:

Step 4: construct window application program with an array of


three textbox controls for input 3 integer and compare to find
largest, smallest and average of the 3 integer.
Design Form:

Coding Form:
Public Class NumberForm
Private Sub BtnMax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMax.Click
Dim input() As TextBox = {txtinput1, txtinput2, txtinput3}
Dim counter As Integer
'declaration
Dim maxi As Integer
For counter = 0 To 2
If input(counter).Text > maxi Then
'comparing the largest number
maxi = input(counter).Text
End If
Next
txtmax.Text = maxi
End Sub

Private Sub BtnMIN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMIN.Click


Dim input1() As TextBox = {txtinput1, txtinput2, txtinput3}
Dim count As Integer
'declaration
Dim min As Integer = 99
For count = 0 To 2
'comparing the smallest number
If min > input1(count).Text Then
min = input1(count).Text
End If
Next
txtmin.Text = min
End Sub
Private Sub btnAve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAve.Click
Dim input2() As TextBox = {txtinput1, txtinput2, txtinput3}
Dim x As Integer
Dim sum As Integer
'declaration
For x = 0 To 2
sum += input2(x).Text
'formula total
Next
txtave.Text = Format(sum / 3, "0.00") 'formula average
End Sub
End Class

Output:

Step 5: write a console application program that allows user to


input examination mark of 10 student and find highest, lowest
and average of 10 student.
Coding Form:
Module Module1
Sub Main()
Dim mark(10) As Integer
Dim high As Integer
Dim low As Integer = 100
Dim sum As Integer

'declaration

For counter As Integer = 0 To 9


Console.Write("Insert your number " & counter + 1 & " student's mark:") 'user insert input number
mark(counter) = Console.ReadLine()
sum += mark(counter)
Next
Console.WriteLine()

For counter = 0 To 9
If mark(counter) > high Then
' comparing the highest mark
high = mark(counter)
End If
Next
Console.WriteLine("The Highest mark of student is " & high & "mark(s).") 'display the output
For counter = 0 To 9
If mark(counter) < low Then
'comparing the lowest mark
low = mark(counter)
End If
Next
Console.WriteLine("The Lowest mark of student is " & low & " mark(s).")
'display the output
Console.WriteLine("The Average mark for the class is " & Format(sum / 10, "0.00") & " mark(s).")
Console.ReadLine()
End Sub
End Module

Output:

Step 6 write a console application program that allow user to


input 10 student in 2 class and comparing to find average mark of
each class, which class get the higher average mark and the
student get highest and lowest mark in 2 class.
Coding Form:
Module Module1
Sub Main()
Dim counterStdnt As Integer
'declaration
Dim counterClass As Integer
Dim mark(2, 10) As Integer
Dim stdntclass As String() = {"DTK5C", "DTK5E"}
'array of class and the student
Dim student(,) As String = New String(1, 9) {{"Ang", "Loh", "Lukman", "Izzat", "Rahimah", "Hazim", "DanaRaj",
"Amir", "Hanafi", "Amirul"},
{"Ramu", "Ali", "Chong", "Lili", "Siti", "Anis", "Ahmad", "Fiza", "Lim", "Kamarul"}}
Dim sum(2) As Integer
Dim average(2) As Integer
Dim high As Integer
Dim low As Integer = 100
Dim max As String
Dim min As String
sum(0) = 0

sum(1) = 0
For counterClass = 0 To 1
'1st looping for the class
Console.WriteLine("{0:s}", stdntclass(counterClass))
For counterStdnt = 0 To 9
'2nd looping for the student
Console.Write("Insert " & student(counterClass, counterStdnt) & "'s mark:")
mark(counterClass, counterStdnt) = Console.ReadLine
sum(counterClass) += mark(counterClass, counterStdnt) 'formula of sum
Next
Console.WriteLine()
Next
'display the output of average and comparing for the highest average between class.
Console.WriteLine("The Average mark of DTK5C is " & Format((sum(0) / 10), "0.00") & " mark(s).")
Console.WriteLine("The Average mark of DTK5E is " & Format((sum(1) / 10), "0.00") & " mark(s).")
If (sum(0) / 10) > (sum(1) / 10) Then
Console.WriteLine("DTK5C obtained highest average mark in final examinataion with " & Format((sum(0) /
10), "0.00") & " mark(s).")
ElseIf (sum(0) / 10) < (sum(1) / 10) Then
Console.WriteLine("DTK5E obtained highest average mark in final examinataion with " & Format((sum(1) /
10), "0.00") & " mark(s).")
End If
For counterClass = 0 To 1
For counterStdnt = 0 To 9
'comparing the highest mark in 2 class
If mark(counterClass, counterStdnt) > high Then
high = mark(counterClass, counterStdnt)
max = student(counterClass, counterStdnt)
End If
Next
Next
Console.WriteLine("The student get highest marks is " & max & " with " & high & " marks(s).")
For counterClass = 0 To 1
For counterStdnt = 0 To 9 'comparing the lowest mark in 2 class
If mark(counterClass, counterStdnt) < low Then
low = mark(counterClass, counterStdnt)
min = student(counterClass, counterStdnt)
End If
Next
Next
Console.WriteLine("The student get lowest marks is " & min & " with " & low & " marks(s).")
Console.ReadLine()
End Sub
End Module

Output:

Discussions:
Design Form:

Coding Form:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim button1(13) As Button
Dim buttonB As Button
Me.BackColor = Color.LightSeaGreen
For i = 0 To 2
button1(i) = New Button()
button1(i).Width = 50
button1(i).Height = 50
button1(i).Top = 100
button1(i).BackColor = Color.Red
button1(i).ForeColor = Color.White
button1(i).Left = (i * 50) + 30
button1(i).Text = i + 1
Me.Controls.Add(button1(i))
AddHandler button1(i).Click, AddressOf button_click
button1(i).BringToFront()
Next
For i = 3 To 5
button1(i) = New Button()
button1(i).Width = 50

button1(i).Height = 50
button1(i).Top = 160
button1(i).BackColor = Color.Gold
button1(i).ForeColor = Color.Black
button1(i).Left = ((i - 3) * 50) + 30
button1(i).Text = i + 1
Me.Controls.Add(button1(i))
AddHandler button1(i).Click, AddressOf button_click
button1(i).BringToFront()
Next
For i = 6 To 8
button1(i) = New Button()
button1(i).Width = 50
button1(i).Height = 50
button1(i).Top = 220
button1(i).BackColor = Color.SkyBlue
button1(i).ForeColor = Color.Black
button1(i).Left = ((i - 6) * 50) + 30
button1(i).Text = i + 1
Me.Controls.Add(button1(i))
AddHandler button1(i).Click, AddressOf button_click
button1(i).BringToFront()
Next
For i = 9 To 12
button1(i) = New Button()
button1(i).Width = 50
button1(i).Height = 50
button1(i).Top = ((i - 9) * 60) + 100
button1(i).BackColor = Color.Violet
button1(i).ForeColor = Color.White
button1(i).Left = (3 * 50) + 50
Me.Controls.Add(button1(i))
button1(i).BringToFront()
Next
button1(9).Text = ("+")
button1(10).Text = ("-")
button1(11).Text = ("*")
button1(12).Text = ("/")
AddHandler button1(9).Click, AddressOf button1_click
AddHandler button1(10).Click, AddressOf button2_click
AddHandler button1(11).Click, AddressOf button3_click
AddHandler button1(12).Click, AddressOf button4_click
buttonB = New Button()
buttonB.Width = 100
buttonB.Height = 50
buttonB.Top = 280
buttonB.BackColor = Color.BlueViolet
buttonB.Left = 50 + 30
Me.Controls.Add(buttonB)
buttonB.Text = (0)
AddHandler buttonB.Click, AddressOf button_click
buttonB.BringToFront()
Me.BackColor = Color.Aquamarine
BtnResult.BackColor = Color.BlueViolet
End Sub
Private Sub button_click(ByVal sender As Object, ByVal e As EventArgs)
txtvalue2.Text = sender.text
End Sub
Private Sub BtnResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
BtnResult.Click
Dim i As Integer = 0
Dim a As Integer
Dim b As Integer

a = txtvalue1.Text
b = txtvalue2.Text
i=a-b
Select Case txtope.Text
Case "+"
i = Val(txtvalue1.Text)
Case "-"
i = Val(txtvalue1.Text)
Case "*"
i = Val(txtvalue1.Text)
Case "/"
i = Val(txtvalue1.Text)

+ Val(txtvalue2.Text)
- Val(txtvalue2.Text)
* Val(txtvalue2.Text)
/ Val(txtvalue2.Text)

Case Else
txtresult.Text = ("Try Again")
End Select
txtresult.Text = i

End Sub
Private Sub button1_click(ByVal sender As Object, ByVal e As EventArgs)
txtvalue1.Text = txtvalue2.Text
txtope.Text = "+"
txtvalue2.Clear()
End Sub
Private Sub button2_click(ByVal sender As Object, ByVal e As EventArgs)
txtvalue1.Text = txtvalue2.Text
txtope.Text = "-"
txtvalue2.Clear()
End Sub
Private Sub button3_click(ByVal sender As Object, ByVal e As EventArgs)
txtvalue1.Text = txtvalue2.Text
txtope.Text = "*"
txtvalue2.Clear()
End Sub
Private Sub button4_click(ByVal sender As Object, ByVal e As EventArgs)
txtvalue1.Text = txtvalue2.Text
txtope.Text = "/"
txtvalue2.Clear()
End Sub
End Class

Output:

Conclusion
From this practical work, I learned that the way of declare the array such as Dim
intStdnt() As Integer it will consist () after the variable to declare it. We also can
declare the textbox using array such as Dim Mark(3) As TextBox =
{Textbox1,TextBox2,TextBox3}. The number in the bracket is show the array we
required. We also can create array using button. First we need to declare 1 st, Dim
Button() As Button then use decorate the button it properties such as font,
backcolor,forecolor and etc. and then using the For..Next loop structure to create as
much as u like. And then Array divided to 2 dimension which is single dimension and
multi dimension for example to keep in the mark of 2 different classes contain of 10
students. Dim mark(2,10) As Integer it mean 2x10 matrix 2 for row and 10 for
column.

You might also like