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.
- 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.
- 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.
- 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.
- 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.
- The text files needed for this assignment should be placed in the bin\debug folder of the project.
- When the form opens, populate the directories listbox with the names of the directories.
- Use an array and IO.ReadAllLines to populate the array with the list of directories.
- 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.)
- 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.
- Before adding a new directory, make sure it doesn’t already exist. You may find it helpful to use Array.IndexOf in your check.
- 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.
- 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.
- Use good programming practices by clearing the listbox and table before refreshing them when a change is made that affects their contents.
- Remember to close the files after accessing them.
- 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
- Use a DataViewGrid control to display the contents of a specific directory.
- Include appropriate error checking (i.e. check for blank names, phone numbers, etc.)
- Use appropriate naming conventions for all controls.
My tutorial contains Visual Basic 2010 Project.
If you have more questions, please contact me via email email@example.com
I will help you any time
Thank you !