Using Visual Basic, create a program that reads the sequential file which you created in Employee Data Part One. Each employee record can be accessed and displayed when the program is run. A new field should be created for the Record Number. When the program first runs, an Input Box should appear requesting the name of the file, which should be Employees.txt (from part one of this assignment). This program displays each of the employee’s records that were saved in the previous assignment. One additional piece of information is added, which is the record number for each employee’s record. The following figure shows the application form.
Once the last record of the file has been displayed, you should display a message indicating to the user that the end of the file has been reached.
Suggested Control Names and Attributes:
|Name Property||Text Property||Container||Control Type||Notes|
|frmEmployeeData2||Employee Data||N/A||Form||Holds Controls|
|lblRecordNum||frmEmployeeData2||Label||Displays the record number|
|grpEmployeeData||Enter Employee Data||frmEmployeeData2||GroupBox||Holds controls for employee data|
|lblFirstName||grpEmployeeData||Label||Employee’s first name|
|lblMiddleName||grpEmployeeData||Label||Employee’s middle name|
|lblLastName||grpEmployeeData||Label||Employee’s last name|
|btnNext||&Next Record||frmEmployeeData2||Button||Triggers event to read next record|
|btnClear||C&lear||frmEmployeeData2||Button||Triggers event to clear a record|
|btnExit||E&xit||frmEmployeeData2||Button||Triggers event to end program|
- Make sure you copy Employees.txt (from part 1) into the bin/Debug folder for your project.
Write the Code:
' Project: Employee Data Part 2 ' Description: This program reads the text file created in Employee Data Part 1 ' It then displays each record to the screen for the user to view. Imports System.IO Public Class frmEmployeeDataPart2 ' Declare global variables Dim recordNumber As Integer = 1 Dim filename As String Dim sr As StreamReader Dim message As String Dim fileValid As Boolean = False Private Sub frmEmployeeDataPart2_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Ask the user for the name of the text file and opens the file ' Checks user input to make sure that the file name is valid Do filename = InputBox("Input Needed", "Enter the name of the file.") If File.Exists(filename) Then sr = File.OpenText(filename) fileValid = True Else message = "Either no file has yet been created or the file " message &= "is not where expected." MessageBox.Show(message, "File Not Found") fileValid = False End If Loop Until fileValid = True End Sub Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click ' clears all of the fields when the Clear button is pressed lblRecordNumber.Text = "" lblFirstName.Text = "" lblMiddleName.Text = "" lblLastName.Text = "" lblEmployeeNumber.Text = "" lblDepartment.Text = "" lblTelephone.Text = "" lblExtension.Text = "" lblEmailAddress.Text = "" End Sub Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click ' Ends the program when the Exit button is pressed. End End Sub Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click If sr.EndOfStream = True Then MessageBox.Show("End of file") Else lblRecordNumber.Text = CStr(recordNumber) lblFirstName.Text = sr.ReadLine lblMiddleName.Text = sr.ReadLine lblLastName.Text = sr.ReadLine lblEmployeeNumber.Text = sr.ReadLine lblDepartment.Text = sr.ReadLine lblTelephone.Text = sr.ReadLine lblExtension.Text = sr.ReadLine lblEmailAddress.Text = sr.ReadLine recordNumber += 1 End If End Sub End Class