Join 136,460 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,758 people online right now. Registration is fast and FREE... Join Now!
I have to read and write a sequential file that contains a rolodex (richtext file). I have 6 textboxes for 6 fields lastName, firstName, Address, ContactDate, City, State, and a maskedTextbox for ZipCode, and richtextbox for notes. I am having a problem writing code for the textboxes that suppose to navigate from one record at a time. Here is my code: 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.
CODE
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)
' 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
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: 7 Aug, 2008 - 11:28 PM
The problem is you are assuming that the file is in the correct format, never assume. Test that is correct before using.
CODE
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 dim Corrent_Size as integer=7 ' 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) if fields.length=Correct_Size Then
What more information you need from the one I gave. Is doesn't work with the coding and explanations you have given. I need to send something here that you can understand what it is I am trying to accomplish, if I am allowed. I have an executable file that shows the actual program and I also have he txt files. Can I include into attachments?
What he's asking is how did it not work? Did you get an error? What did it do versus what you need it to do? Saying It didn't work just doesn't give us much to work with
Oh sorry, I misinterpreted. I tried what you told me to include in the function return nothing, I did but it still renders the same previous error. "Index was outside the bounds of the array." That was the previous error that made me seek for help in this forum.
dim rec as Rolodex=ReadContact If IsNothing(rec) = True Then ' There as problem Else ' Normal processing End If
Added debug information to function
CODE
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 dim Corrent_Size as integer=7 ' 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) if fields.length=Correct_Size Then
' Return the contact record. Return CurrentContact else ' Debug information debug.print("Number of fields: {0}",fields.length) return Nothing endif End Function
This post has been edited by AdamSpeight2008: 13 Aug, 2008 - 04:15 PM
I also reckon on that there is a blank line in the file,most likely the very last one.
Ok, so far there is no error showing, but does not load to the textboxes.
Debug information is displayed in the output window, it doesn't display a normal error box.
Textboxes? What textboxes? You've only just mentioned textboxes.
The textboxes where the information is displayed. I have a form with 6 textboxes, one maskedtextbox, and a richtextbox. I mentioned that in my first post and part of my coding as well.