Create an application that reads the records stored in the file created by Progr
ID: 3582793 • Letter: C
Question
Create an application that reads the records stored in the file created by Program-ming Challenge 1. Write code in the form’s Load event handler that allows the user to enter the name of the file, and opens the file. The form shown in Figure 9- 30 has a Next Record button, which reads a record from the file and displays its fields; a Clear button, which clears the labels; and an Exit button. When the user clicks the Next Record button, the application should read the next record from the file and display it. When the end of the file is encountered, a message should be displayed.
Explanation / Answer
Solution:
Declare streamwriter
Dim EmployeeFile As StreamWriter
If File.Exists(strInputFileName) Then
' Create the file
EmployeeFile = File.AppendText(strInputFileName)
' Write our lines of text
EmployeeFile.WriteLine(txtFirstName.Text)
EmployeeFile.WriteLine(txtMiddleName.Text)
EmployeeFile.WriteLine(txtLastName.Text)
EmployeeFile.WriteLine(txtEmpNumber.Text)
EmployeeFile.WriteLine(cmbDept.SelectedItem.ToString())
EmployeeFile.WriteLine(txtPhone.Text)
EmployeeFile.WriteLine(txtPhoneExt.Text)
EmployeeFile.WriteLine(txtEmail.Text)
EmployeeFile.Close()
' Tell the user that the file was updated
MessageBox.Show("The data was added and the file " &
strInputFileName & " was Next Record button.")
Else
' Create the file
EmployeeFile = File.CreateText(strInputFileName)
EmployeeFile.WriteLine(txtFirstName.Text)
EmployeeFile.WriteLine(txtMiddleName.Text)
EmployeeFile.WriteLine(txtLastName.Text)
EmployeeFile.WriteLine(txtEmpNumber.Text)
EmployeeFile.WriteLine(cmbDept.SelectedItem.ToString())
EmployeeFile.WriteLine(txtPhone.Text)
EmployeeFile.WriteLine(txtPhoneExt.Text)
EmployeeFile.WriteLine(txtEmail.Text)
EmployeeFile.Close()
' Tell the user that the file was created
MessageBox.Show("The file " & strInputFileName & " was Next
Record button.")
End If
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.