Adapted from Introduction to Programming Using Visual Basic 2012, 9/E, David I. Schneider.
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.
a. 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 seleted 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.
b. 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.
c. Delete a name (as specified in the “Name” text box) from the current phone directory. This action should update the text file containing the listings of the current directory. As is good software development practice, you should prompt the user with a messagebox that allows him to confirm the deletion before any permanent action is taken to delete the entry from the file. The grid should also refresh to show the removal of the listing.
d. Display the names and phone numbers in the current phone directory. (Note that the listings are displayed in ascending order in the table.)
Design your form as depicted in the diagram below.
1. The text files needed for this assignment should be placed in the bin\debug folder of the project.
2. When the form opens, populate the directories listbox with the names of the directories.
3. Use an array and IO.ReadAllLines to populate the array with the list of directories.
4. Use the StreamWriter in conjunction with IO.File.AppendText and IO.File.CreateText to append new directories and create new text files. (Note: You will need to use the WriteLine method to write the name of the new file to Directories.txt.)
5. When the user clicks the Create a New Phone Directory button, prompt him to enter the name using an InputBox. If the user fails to enter a name, display a message to him (i.e. “Invalid directory name”), and do not proceed to create a new directory. If the operation is successful, make the new directory the current directory.
6. Before adding a new directory, make sure it doesn’t already exist. You may find it helpful to use Array.IndexOf in your check.
7. To display the contents of the directories file, use a LINQ query. Loop over all of the items in the query using a For each statement to populate the listbox.
8. Before displaying the contents of the directories file, make sure the file exists first. You will need to use the IO.File.Exists method in your check.
9. Use good programming practices by clearing the listbox and table before refreshing them when a change is made that affects their contents.
10. Remember to close the files after accessing them.
11. To remove a listing from a directory, you may find this code helpful as it selects only those records in the file that are not the one the user wants to remove. The code should then write the results of this query back to the file:
Dim query = From line In IO.File.ReadAllLines(fileName)
Let name = line.Split(","c)(0)
Let phoneNum = line.Split(","c)(1)
Where name <> txtName.Text
Select name & "," & phoneNum
12. Use a DataViewGrid control to display the contents of a specific directory.
13. Include appropriate error checking (i.e. check for blank names, phone numbers, etc.)
14. Use appropriate naming conventions for all controls.