I am trying to write a program that would create and maintain telephone directories. Here are the instructions and I crossed out what I did already. I have it all set up but I still don't get the results I need, I press the buttons and nothing happens. Please help with whatever step you can, I will really appreciate it!
-
- 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.)
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 Class

New Topic/Question
Reply



MultiQuote






|