I am trying to create a phone book program in Visual Basic. I have so many phone numbers, I thought this would be a neat way to handle the overflow lol. I want it to read the information from a text file with the following data: First name, last name, home phone, cell phone, and e-mail address (with each item of data on its own line, so that each person listed will have five lines with no blank lines between individuals) If any data is missing or does not exist its corresponding line is to be blank.

I want it to be where the data can be searched in many different ways. An individual may be called up by sequence number, and the user is also to be able to browse the names with First, Last, Next, and Previous buttons. And I'm guessing a find button to find the next occurrence which matches the text entered in the search text box.

So far I have whats below, my main problem is that my first name ends up coming in twice and then everything gets pushed down one thing so my email address isn't on there...Can someone please help me :)

Thanks:)

{Imports System.IO
Imports str = Microsoft.VisualBasic
Public Class Form1
Dim InpFileName As String, FileOpenFlag As Integer
Dim FirstName(25), LastName(25), HomePhone(25), CellPhone(25), Email(25) As String
Dim Count, Index As Integer
Dim DataFile As StreamReader


Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
' Get name of file to read and open file
Dim dgrResult As DialogResult
Dim dlgOpen As New OpenFileDialog
If FileOpenFlag > 0 Then DataFile.Close()
' Set properties and open file dialog
dlgOpen.InitialDirectory = "F:"
dlgOpen.Filter = "Text Files|*.txt|Word Files|*.doc|All Files|*.*"
dgrResult = dlgOpen.ShowDialog()


' If file name OK then open file for reading
If dgrResult = DialogResult.OK Then
InpFileName = dlgOpen.FileName      ' Get filename
DataFile = File.OpenText(InpFileName)
FileOpenFlag = 1
Count = 0
ReadData()
DataFile.Close()
Else
TxtFirstName.Text = "File not specified"
End If
End Sub


Private Sub ReadData()
' Read all the items in the file
Dim Temp As String
If FileOpenFlag > 0 Then
Do While DataFile.Peek <> -1
Temp = DataFile.ReadLine
If Temp.Trim <> "" Then
Count = Count + 1
FirstName(Count) = Temp.Trim
LastName(Count) = Temp.Trim
HomePhone(Count) = DataFile.ReadLine
CellPhone(Count) = DataFile.ReadLine
Temp = DataFile.ReadLine
Email(Count) = DataFile.ReadLine
Temp = DataFile.ReadLine
Else
Exit Do
End If
Loop
If Count > 0 Then
Index = 1
DisplayData()
End If
End If
End Sub


Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
If IsNumeric(txtItemNumber.Text) Then
Index = CInt(txtItemNumber.Text)
If Index > 0 And Index <= Count Then
DisplayData()
Else
Index = 0
DisplayData()
TxtFirstName.Text = "Invalid number"
End If
End If
End Sub
Private Sub DisplayData()
txtItemNumber.Text = CStr(Index)
TxtFirstName.Text = FirstName(Index)
txtlastname.Text = LastName(Index)
txtphone.Text = HomePhone(Index)
txtphone2.Text = CellPhone(Index)
txtemail.Text = Email(Index)
End Sub


Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
If Index < Count Then
Index = Index + 1
DisplayData()
End If
End Sub


Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
If Index > 1 Then
Index = Index - 1
DisplayData()
End If
End Sub


Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
Dim FindString, CurrentString As String
Dim FindLength As Integer, N As Integer
FindString = txtFind.Text
FindString = FindString.Trim
FindString = FindString.ToUpper
FindLength = FindString.Length
If Index < Count Then
For N = Index + 1 To Count
CurrentString = FirstName(N).Trim
CurrentString = CurrentString.ToUpper
CurrentString = str.Left(CurrentString, FindLength)
If FindString = CurrentString Then
Index = N
DisplayData()
Exit For
Else
If N = Count Then
MessageBox.Show("End of list; search text not found")
End If
End If
Next
Else
MessageBox.Show("End of list; search text not found")
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If FileOpenFlag > 0 Then DataFile.Close()
End Sub
End Class
}

Edited 3 Years Ago by happygeek: fixed formatting

You are getting the first name twice because of this:
FirstName(Count) = Temp.Trim
LastName(Count) = Temp.Trim
Try this:
FirstName(Count) = Temp.Trim
Temp = DataFile.ReadLine
LastName(Count) = Temp.Trim

Yep that worked!!! Now I am trying to have two buttons first and last. So when I am down in the line it will either go to the very first entry or go to the very last entry. How would you do that... so far this is what I have and they are not working!!

{ Private Sub BtnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFirst.Click
If Index > Count Then
Index = Index - Count + 1
DisplayData()
End If
End Sub


Private Sub BtnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLast.Click
If Index < Count Then
Index = Index + Count - 1
DisplayData()
End If
End Sub
}

can you help me here at all?

THanks :)

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.