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

Lecture 03

The document provides a tutorial on creating Visual Basic Windows Forms Applications focusing on conditional statements, loops, and factorial calculations. It includes step-by-step instructions for programming grading systems based on marks, displaying messages based on grades, and generating integers using different looping constructs. Additionally, it covers how to calculate the factorial of a user-entered number with appropriate error handling for negative inputs.

Uploaded by

ishuwaraf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views23 pages

Lecture 03

The document provides a tutorial on creating Visual Basic Windows Forms Applications focusing on conditional statements, loops, and factorial calculations. It includes step-by-step instructions for programming grading systems based on marks, displaying messages based on grades, and generating integers using different looping constructs. Additionally, it covers how to calculate the factorial of a user-entered number with appropriate error handling for negative inputs.

Uploaded by

ishuwaraf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

VISUAL PROGRAMMING

T H A R I K A W E E R A KO O N - D C S

07/29/2025 COM323Α 1
CONDITIONAL
STATEMENTS

07/29/2025 COM323Α 2
1.
a) Start a new Visual Basic Windows Forms Application project.
b) Add label stating, “Enter your marks”.
c) Add a textbox in front of the label.
d) Add a button stating, “Check” to the form and program it according
to the steps given below.
e) After marks entered to the textbox, the student should be able to
print his/her grading considering the following criteria.
• Marks > = 90 => A+
• Marks > = 80 => A
• Marks > = 70 => B
• Marks > = 60 => C
• Marks > = 50 => D
• Marks < 50 => F

07/29/2025 COM323Α 3
Answer

07/29/2025 COM323Α 4
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim marks As Integer
marks = TextBox1.Text
If (marks >= 90) Then
MessageBox.Show("A+")
ElseIf (marks >= 80) Then
MessageBox.Show("A")
ElseIf (marks >= 70) Then
MessageBox.Show("B")
ElseIf (marks >= 60) Then
MessageBox.Show("C")
ElseIf (marks >= 50) Then
MessageBox.Show("D")
Else
MessageBox.Show("F")
End If
End Sub
End Class
07/29/2025 COM323Α 5
2.
a) Start a new Visual Basic Windows Forms Application project.
b) Add label stating, “Enter Grading”.
c) Add a textbox in front of the label.
d) Add a button stating, “Check” to the form and program it according to
the steps given below.
e) After marks entered to the textbox, the student should be able to print
his/her grading considering the following criteria.
• Grade is A => Excellent
• Grade is B or C => Well Done
• Grade is D => You Passed
• Grade is F => Better Try Again

07/29/2025 COM323Α 6
Answer

07/29/2025 COM323Α 7
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim grade As Char
grade = TextBox1.Text
Select Case grade
Case "A"
MessageBox.Show("Excellent!")
Case "B", "C"
MessageBox.Show("Well done")
Case "D"
MessageBox.Show("You passed")
Case "F"
MessageBox.Show("Better try again")
Case Else
MessageBox.Show("Invalid grade")
End Select
End Sub
End Class

07/29/2025 COM323Α 8
3.

Output:

07/29/2025 COM323Α 9
Answer
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End
End Sub

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


Dim fruit As String
fruit = " "
If CheckBox1.Checked = True Then
fruit = "Apple"
End If
If CheckBox2.Checked = True Then
'fruit = CheckBox2.Text
fruit = fruit & " Mango"
End If
If CheckBox3.Checked = True Then
fruit = fruit & " Banana"
End If
If CheckBox4.Checked = True Then
fruit = fruit & " Orange"
End If
If CheckBox5.Checked = True Then
fruit = fruit & " Potato"
End If
If CheckBox6.Checked = True Then
fruit = fruit & " Tomato"
End If
If fruit.Length <> 0 Then
MessageBox.Show(" Selected items " & fruit)
End If
CheckBox1.ThreeState = True
End Sub
End Class

07/29/2025 COM323Α 10
LOOPS

07/29/2025 COM323Α 11
1)
a) Start a new Visual Basic Windows Forms Application project.
b) Add a button to the form and program it according to the steps given below.
c) Generate integers from 1 to 10 using a While…End While statement.
d) Display the generated integers in message boxes.

07/29/2025 COM323Α 12
Answer

07/29/2025 COM323Α 13
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
Dim i As Integer
i = 1
While (i <= 10)
MessageBox.Show(i)
i = i + 1
End While
End Sub
End Class

07/29/2025 COM323Α 14
2)
a) Start a new Visual Basic Windows Forms Application project.
b) Add a button to the form and program it according to the steps given below.
c) Generate integers from 1 to 10 using a Do…Loop statement with While keyword.
d) Display the generated integers in message boxes.

07/29/2025 COM323Α 15
Answer

07/29/2025 COM323Α 16
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs)
Handles Button1.Click
Dim i As Integer
i = 1
Do
MessageBox.Show(i)
i = i + 1
Loop While (i <= 10)
End Sub
End Class
07/29/2025 COM323Α 17
3)
a) Start a new Visual Basic Windows Forms Application project.
b) Add a button to the form and program it according to the steps given below.
c) Generate integers from 1 to 10 using a Do…Loop statement with Until keyword.
d) Display the generated integers in message boxes.

07/29/2025 COM323Α 18
Answer

07/29/2025 COM323Α 19
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs)
Handles Button1.Click
Dim i As Integer
i = 1
Do
MessageBox.Show(i)
i = i + 1
Loop Until (i > 10)
End Sub
End Class
07/29/2025 COM323Α 20
3)
a) Start a new Visual Basic Windows Forms Application project.

b) Add a Label stating, “Enter Number” and a Textbox.

c) Add a button to the form and program it according to the steps given below.

d) Calculate the factorial of user entered number.


• When user enters a negative value, the factorial will not be calculated (Message Box will show
“Factorial of negative number is not possible”)
• Factorial of 0 and 1 is 1

07/29/2025 COM323Α 21
Answer

07/29/2025 COM323Α 22
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim n, i, f As Integer
n = TextBox1.Text
f = 1
If n < 0 Then
MessageBox.Show("Factorial of negative number is not possible")
ElseIf n = 0 Or n = 1 Then
MessageBox.Show("Factorial of " + n.ToString() + " is 1")
Else
For i = 1 To n
f *= i
Next
MessageBox.Show("Factorial of " + n.ToString() + " is " + f.ToString())
End If
End Sub
End Class

07/29/2025 COM323Α 23

You might also like