I0060
08 Laboratory Exercise 1
Object-Oriented Programming Concepts
Objectives:
At the end of the exercise, the students should be able to:
create a class, and
call a class as an object in an application.
Materials:
Computer with installed Microsoft Visual Basic 2008 Express Edition
Flash drive
Basic Principles:
This activity will introduce to the students on how to create class in project development. This will
give the students idea for the next topic to be discussed.
Procedures:
Creating a Class in a Windows Application Project
1. Hit Start > All Programs > Microsoft Visual Basic 2008 to start the application. Create a
new project and name it as OOP.
2. On the Form1 window, add six (6) Labels, six (6) TextBoxes and two (2) Buttons.
3. Arrange the controls in the form as below.
Figure 8.1 Form1
4. Set the Name property of the textboxes same as their caption labels. For example,
txtStudentID for “Student ID”.
5. Set also the Name property of button Check Entries to btnCheckEntry and button Exit to
btnExit.
08 Laboratory Exercise 1 *Property of STI
Page 1 of 3
I0060
Creating the Class
6. On the Project menu, click Add Class.
7. Name the Class as ValidateTextBox.
8. Under Public Class ValidateTextBox code window, write the following code:
Public Function IsTextBoxEmpty(ByVal txt As TextBox) As Boolean
If txt.Text.Trim = "" Then
txt.Focus()
Return True
Else
Return False
End If
End Function
Public Function IsTextBoxNumeric(ByVal txt As TextBox) As Boolean
If IsNumeric(txt.Text.Trim) Then
txt.Focus()
Return True
Else
Return False
End If
End Function
Adding the codes to the Form1
9. Under Public Class Form1 code window, write the following code:
Private Sub btnCheckEntry_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnCheckEntry.Click
'creating an instance of the class Validate Textbox
Dim clsValidate As New ValidateTextBox
'validating the value of txtStudentID
If clsValidate.IsTextBoxEmpty(txtStudentID) = True Then
Msgbox("Please enter Student ID.")
Exit Sub
End If
'validating the value of txtFirstName
If clsValidate.IsTextBoxEmpty(txtFirstName) = True Then
Msgbox("Please enter First Name.")
Exit Sub
End If
'validating the value of txtMiddleName
If clsValidate.IsTextBoxEmpty(txtMiddleName) = True Then
Msgbox("Please enter Middle Name.")
Exit Sub
End If
'validating the value of txtLastName
If clsValidate.IsTextBoxEmpty(txtLastName) = True Then
Msgbox("Please enter Last Name.")
Exit Sub
End If
'validating the value of txtAge
If clsValidate.IsTextBoxNumeric(txtAge) = False Then
Msgbox("Please enter a valid age.")
Exit Sub
08 Laboratory Exercise 1 *Property of STI
Page 2 of 3
I0060
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnExit.Click
Close()
End Sub
10. Run the program by pressing <F5>.
11. Click the Check Entries button and try to input values on each text box.
NOTE: A prompt must appear when any of the textboxes is left empty and when the entry
in the Age textbox is a letter.
12. Click Exit button to stop the program.
13. Save your work.
I. Challenge Exercises:
1. Modify the exercise above and come up with the following conditions for each Textbox. The
conditions should be created as Class.
o Student ID – Validate for Null (empty) value and numeric value. A prompt must appear
to inform user that this field cannot be empty or contain any value other than number.
o First/Middle/Last Name – Validate for Null (empty) value and 30 maximum length of
characters. A prompt must appear to inform user that these fields cannot be empty and
value entered should not exceed 30 characters.
2. Save the laboratory exercise.
08 Laboratory Exercise 1 *Property of STI
Page 3 of 3