I have a program that is supposed to create and maintain tel dir, it uses arrays
ID: 440670 • Letter: I
Question
I have a program that is supposed to create and maintain tel dir, it uses arrays and txt files, I have code for it but i have errors and i am not sure where... here are the instructions for the code, and my code will follow, any advice would be greatly appriciated. Thanks. PROGRAMMING INSTRUCTIONS Create and Maintain Telephone Directories Write a Visual Basic program to create and maintain telephone directories. Each telephone directory should be contained in a separate text file. In addition, a file named Directories.txt should hold the names of the telephone directories. At any time, names of all the telephone directories should be displayed in a list box. After a telephone directory is selected, it becomes the current phone directory. The following buttons should be available. 1. Create a new telephone directory. (The filename should be provided by an input dialog box.) A new text file should be created for the directory, and the name of the new directory should be added to the Directories.txt file. The Phone Directories listbox should immediately be refreshed to reflect the new directory, and the new directory should be automatically selected from the listbox. Note that this selection should trigger the procedure that sets the current directory and updates the contents of the current directory textbox. Also note that the list of directories is displayed in ascending order in the listbox. 2. Add a listing (as given in textboxes) to the end of the current phone directory. This action should update the text file containing the listings of the current directory and refresh the grid to display the newly added listing. 3. Delete a name (as specified in theExplanation / Answer
Public Class frmTelephoneDirectories Structure people Dim name As String Dim number As String End Structure Structure Directory Dim File As String Dim records() As people Dim recordCount As Integer End Structure Dim directories() As Directory Dim directoryCount As Integer = 0 Dim temp As String Dim tempreader As IO.StreamReader Dim tempBuffer() As String Dim position As Integer Private Sub btnCreateNew_Click(sender As System.Object, e As system.EventArgs) Handles btnCreateNew.Click Dim directoryName As String = InputBox("Enter a directory name") Dim writer As IO.StreamWriter directoryCount += 1 ReDim Preserve directories(directoryCount) directories(directoryCount - 1).File = directoryName writer = IO.File.AppendText("directories.txt") writer.WriteLine(directoryName) lstTextFiles.Items.Add(directoryName) If directoryName = "" Then MessageBox.Show("Please enter in the directory name!") End If End Sub Private Sub btnAddListing_Click(sender As System.Object, e As System.EventArgs) Handles btnAddListing.Click Dim name As String = "" Dim number As String = "" name = txtName.Text number = txtPhone.Text directories(lstTextFiles.SelectedIndex).recordCount += 1 ReDim Preserve directories(lstTextFiles.SelectedIndex).records(directories(lstTextFiles.SelectedIndex).recordCount) directories(lstTextFiles.SelectedIndex).records(directories(lstTextFiles.SelectedIndex).recordCount - 1).name = name directories(lstTextFiles.SelectedIndex).records(directories(lstTextFiles.SelectedIndex).recordCount - 1).number = number End Sub Private Sub frmTelephoneDirectories_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Dim sw As IO.StreamWriter = IO.File.CreateText("Directories.txt") sw.WriteLine("Friends.txt") sw.WriteLine("Merchants.txt") sw.WriteLine("Restaurants.txt") sw.WriteLine("Theaters.txt") sw.Close() Dim sr As IO.StreamReader = IO.File.OpenText("Directories.txt") lstTextFiles.Items.Clear() Do While sr.Peek -1 temp = sr.ReadLine() lstTextFiles.Items.Add(temp) ReDim Preserve directories(lstTextFiles.Items.Count()) directories(lstTextFiles.Items.Count() - 1).File = temp tempreader = IO.File.OpenText(temp) While (tempreader.Peek() -1) temp = tempreader.ReadLine() tempBuffer = temp.Split(","c) position = lstTextFiles.Items.Count() - 1 directories(position).recordCount += 1 ReDim Preserve directories(position).records(directories(position).recordCount) directories(position).records(directories(position).recordCount - 1).name = tempBuffer(0) directories(position).records(directories(position).recordCount - 1).number = tempBuffer(1) End While tempreader.Close() Loop sr.Close() End Sub Private Sub lstFiles_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstTextFiles.SelectedIndexChanged txtDirectory.Text = CStr(lstTextFiles.SelectedItem) End Sub Private Sub btnRemoveListing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveListing.Click Dim tempData() As people Dim name As String = txtName.Text Dim position As Integer = 0 Dim counter As Integer = 0 For i As Integer = 0 To directories(lstTextFiles.SelectedIndex).records.GetUpperBound(0) - 1 If (directories(lstTextFiles.SelectedIndex).records(i).name name) Then counter += 1 ReDim Preserve tempData(counter) tempData(counter - 1).name = directories(lstTextFiles.SelectedIndex).records(i).name tempData(counter - 1).number = directories(lstTextFiles.SelectedIndex).records(i).number End If Next directories(lstTextFiles.SelectedIndex).records = tempData End Sub Private Sub btnDisplayListings_Click(sender As System.Object, e As System.EventArgs) Handles btnDisplayListings.Click If (directories(lstTextFiles.SelectedIndex).records Is Nothing) Then DgvDirectory.DataSource = Nothing Return End If Dim directoryQ = From data In directories(lstTextFiles.SelectedIndex).records Select data.name, data.number Order By name DgvDirectory.DataSource = directoryQ.ToList() End Sub End ClassRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.