3 Replies - 4227 Views - Last Post: 02 October 2012 - 12:38 PM Rate Topic: -----

#1 compucrazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-September 12

Help with telephone directory program...almost done!

Posted 02 October 2012 - 12:11 PM

Hi!

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!


- 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.)



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


Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Help with telephone directory program...almost done!

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9498
  • View blog
  • Posts: 35,845
  • Joined: 12-June 08

Re: Help with telephone directory program...almost done!

Posted 02 October 2012 - 12:23 PM

Quote

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.

Have you put a break point in your buttons and checked out what is going on in them?

Quote

Please help with whatever step you can, I will really appreciate it!

I'll stop you right there and tell you we will not do your homework for you.
Was This Post Helpful? 0
  • +
  • -

#3 compucrazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 30-September 12

Re: Help with telephone directory program...almost done!

Posted 02 October 2012 - 12:34 PM

I didn't want you to do my homework, the program is practically done. I just wanted help fixing the few errors that I was getting.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9498
  • View blog
  • Posts: 35,845
  • Joined: 12-June 08

Re: Help with telephone directory program...almost done!

Posted 02 October 2012 - 12:38 PM

Then you need to explain what those errors are (error text, line number, etc), and not just rattle off your homework's requirements. Just doing the latter makes it appear as if you want someone to do the rest for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1