K:\TPG14BT_CT1_Students\LAB1027B-PC20\212247200\TPG14BT_CT1_2014S2\staffCards.
vb 21 August 2014 03:15 PM
Imports System.IO
' Student number: 212247200
' Student name: MF Mafalo
Public Class frmMain
Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
' 13 marks
' Question 1
Dim myFile As StreamReader 'declaring variable for text file
Dim intStaffCount As Integer = 0 ''counts the number of staff members read from the
textfile at an initial value of 0
'verification that the text file exists using if statement
'if the file is not found then the rest of the code will not be executed thus the
rest of code is kept within the IfElse statement
If File.Exists("..\..\staff.txt") = False Then
'error message if file does not exist
MessageBox.Show("File does not exist", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Else
''file is found end the rest of the code is processed in order to manipulate the
contents of the textfile
''open file if found
myFile = File.OpenText("..\..\staff.txt")
''read lines from textfile using the peek function
Do While myFile.Peek <> -1
''display data from textfile on to listbox
lstStaff.Items.Add(myFile.ReadLine)
''count staff members read from text file
intStaffCount += 1
''display number of staff memebrs counted onto label
lblCount.Text = "There are " & CStr(intStaffCount) & " staff members!"
Loop
''close the file
myFile.Close()
End If
End Sub
Private Sub lstStaff_SelectedIndexChanged(sender As Object, e As EventArgs) Handles
lstStaff.SelectedIndexChanged ' 20 marks
' Question 2
Dim strLine As String 'used to store the selected line from index
Dim lngIdNo As Long 'stores the id number
Dim intYear As Integer 'stores the year of staff
Dim intPos As Integer 'stores the position of the hash tag
Dim intAge As Integer 'stores the age of the staff
Dim intGender As Integer 'stores the 7th character of the gender
Dim strSurname As String 'stores the surname of the staff
Dim strInit As String 'stores the initials of the staff
Open Rubric -1-
K:\TPG14BT_CT1_Students\LAB1027B-PC20\212247200\TPG14BT_CT1_2014S2\staffCards.vb 21 August 2014 03:15 PM
''retrieval of the selected line from index
strLine = lstStaff.SelectedItem
''find the position of the hash
intPos = strLine.IndexOf("#")
''extract the id no and converts the string to long
lngIdNo = CLng(strLine.Substring(0, intPos))
''extract the year from id and converts it to integer
intYear = CInt("19" & lngIdNo.ToString.Substring(0, 2))
''calculate the age
intAge = DateAndTime.Now.Year - intYear
''displays the age to label, converts age to string
lblAge.Text = "AGE: " & CStr(intAge)
''extract the 7th number for gender and convert to integer
intGender = CInt(lngIdNo.ToString.Substring(6, 1))
'using the ifElse statement to determine whether the staff is male or female
If intGender < 5 Then
lblGender.Text = "GENDER: Female" ''displays for male
Else
lblGender.Text = "GENDER: Male" ''displays for female
End If
''remove id no from line to separate from name and surname
strLine = strLine.Remove(0, intPos + 1)
''find position of the comma after the surname
intPos = strLine.IndexOf(",")
''extracts the surname and displays to the label
strSurname = strLine.Substring(0, intPos)
lblSurname.Text = "SURNAME: " & strSurname
''extracts the initials and displays to the label
strInit = strLine.Substring(intPos + 1)
lblInitials.Text = "INITIALS: " & strInit
Dim filePath As String = "..\..\PHOTOS\" 'variable for file path to the photos folder
''concatinate the filepath variable aswell as the surname and initial variables to
create a full path name for the image needed
picPhoto.BackgroundImage = Image.FromFile(filePath & strSurname & strInit & ".jpg")
End Sub
End Class
-2-
RESULTS
Total = 30 / 30 (100%)
Finalize