I appreciate for your help. This is written in Visual Basic 2005 from the book Programming with Microsoft VB 2005 Object Oriented Approach byy Michael Ekedahl, 2007.
Option Explicit On
Option Strict On
Imports System.Convert
Imports System.IO
' The ContactReader class is used to read the
' sequential file containing the contacts.
Public Class RolodexList
Private CurrentReader As StreamReader
' Create an instance of the StreamReader when the
' class instance is created.
Public Sub New(ByVal argFile As String)
CurrentReader = New StreamReader(argFile)
End Sub
' Read the contact file.
Public Function ReadContact() As Rolodex
Dim CurrentContact As Rolodex
Dim Fields() As String
Dim CurrentRecord As String
' Define the delimiter character.
Dim DelimiterChars() As Char = {ToChar(",")}
' Read a line from the file.
CurrentRecord = CurrentReader.ReadLine()
' Split the line into the respective fields.
Fields = CurrentRecord.Split(DelimiterChars)
' Populate the contact record.
With CurrentContact
.lastName = Fields(0)
IndexOutOfRangeException---> .firstName = Fields(1) <-- My error
.Address = Fields(2)
.ContactDate = ToDateTime(Fields(3))
.City = Fields(4)
.State = Fields(5)
.ZipCode = Fields(6)
End With
' Return the contact record.
Return CurrentContact
End Function
' Determine whether end of file is true by
' trying to examine the next character. End of file
' is true if there are no more characters.
Function EndOfFile() As Boolean
Dim NextCharacter As Integer
NextCharacter = CurrentReader.Peek()
If NextCharacter = -1 Then
Return True
End If
End Function
' Close the file referenced by CurrentReader.
Public Sub Close()
CurrentReader.Close()
End Sub
End Class
' Declare the structure named Contact to store
' the contact information.
Public Structure Rolodex
Public lastName As String
Public firstName As String
Public Address As String
Public ContactDate As Date
Public City As String
Public State As String
Public ZipCode As String
End Structure
Option Explicit On
Option Strict On
Imports System.IO
Imports System.Convert
Public Class Form1
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If My.Computer.Keyboard.NumLock = True Then
tsslNumLock.Text = "Num"
Else
tsslNumLock.Text = ""
End If
If My.Computer.Keyboard.CapsLock = True Then
tsslCapsLock.Text = "Caps"
Else
tsslCapsLock.Text = ""
End If
If My.Computer.Keyboard.ScrollLock = True Then
tsslScrollLock.Text = "Scroll"
Else
tsslScrollLock.Text = ""
End If
End Sub
Private Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click
Dim Result As DialogResult
Result = colorDialog.ShowDialog
If Result = Windows.Forms.DialogResult.OK Then
rtbMain.SelectionColor = colorDialog.Color
End If
End Sub
Private Sub FontToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click
Dim result As DialogResult
result = fontDialog.ShowDialog
If result = Windows.Forms.DialogResult.OK Then
rtbMain.SelectionFont = fontDialog.Font
End If
End Sub
Private Sub OpenFile()
Dim Result As DialogResult
openDialog.Filter = "Rich Text(*.rtf)|*.rtf" & _
"Text Files(*.txt)|(*.txt)|*.txt|All Files(*.*)|*.*"
openDialog.FilterIndex = 2
Result = openDialog.ShowDialog
If Result = Windows.Forms.DialogResult.OK Then
rtbMain.LoadFile(openDialog.FileName, _
RichTextBoxStreamType.PlainText)
End If
tsslFileName.Text = openDialog.FileName
End Sub
Private Sub SaveFile()
Dim result As DialogResult
result = saveDialog.ShowDialog
If result = Windows.Forms.DialogResult.OK Then
rtbMain.SaveFile(saveDialog.FileName, _
RichTextBoxStreamType.RichText)
End If
End Sub
Private Sub NavigateFirst()
rtbMain.Selectionstart = 0
rtbMain.SelectionLength = 0
End Sub
Private Sub NavigateLast()
rtbMain.Selectionstart = rtbMain.Text.Length - 1
End Sub
Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpen.Click
Call OpenFile()
End Sub
Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click
Call SaveFile()
End Sub
Private Sub tsFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsFirst.Click
Call NavigateFirst()
End Sub
Private Sub tsLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsLast.Click
Call NavigateLast()
End Sub
Private CurrentRolodexList() As Rolodex
Private CurrentRecord As Integer = 0
Private NextRecord As Integer = 0
Private Sub tsOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsOpen.Click
Dim CurrentReader As RolodexList
Dim CurrentContact As Rolodex
Dim Result As DialogResult
openDialog.Filter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*"
Result = openDialog.ShowDialog
If Result = Windows.Forms.DialogResult.OK Then
' Create the instance of the ContactReader class thereby creating
' the underlying StreamReader
CurrentReader = New RolodexList(openDialog.FileName)
' Read and proces until the end of file has been reached.
Do Until CurrentReader.EndOfFile
CurrentContact = CurrentReader.ReadContact
ReDim Preserve CurrentRolodexList(NextRecord)
CurrentRolodexList(NextRecord) = CurrentContact
NextRecord += 1
Loop
CurrentReader.Close()
End If
Call DisplayCurrentRecord(CurrentRecord)
End Sub
Private Sub DisplayCurrentRecord(ByVal index As Integer)
txtfirstName.Text = CurrentRolodexList(index).firstName
txtlastName.Text = CurrentRolodexList(index).lastName
txtAddress.Text = CurrentRolodexList(index).Address
txtContactDate.Text = CurrentRolodexList(index).ContactDate.ToShortDateString
txtCity.Text = CurrentRolodexList(index).City
txtState.Text = CurrentRolodexList(index).State
mtbZipCode.Text = CurrentRolodexList(index).ZipCode
End Sub
End Class
This post has been edited by bravo659: 07 August 2008 - 11:28 PM


Reply






MultiQuote






|