I need to make my program display a few sets of variables, with different details, on my form. This is not the hard part however as the hard part is when it gets to the end of the file my entire program breaks down! Now I can manually set it so that the program checks to see if a certain number of lines has been read and THEN stop close and reopen the file but if I do that then I won't be able to edit the file without going into the code itself and changing the amount of lines the program has to look for.
There are only two lines that are really giving me trouble (I have labeled them in the code).
I tried using the EOF function but that didn't work.
Is there any other way that I can make the program check to see if the text file has completed itself and then close and reopen it so I can use the lines in the text file again?
Here is a picture of the Form that I am working on!
Here is the code:
Public Class frmMain
Dim CurrentDisplayLine As Integer = 0
Private Sub btnNextStudent_Click(sender As Object, e As EventArgs) Handles btnNextStudent.Click
Dim TempLine As String
Dim TempString As String = ""
Dim FileNum As Integer = FreeFile()
Dim EndLineNum As Integer = CurrentDisplayLine + 5 'Make The EndLineNum (Limit) equal the CurrentDisplayLine PLUS "5" to it
Dim I As Integer = 0
FileOpen(FileNum, "enrolments.txt", OpenMode.Input)
If EndLineNum = 30 Then 'If the EndLineNum equals "30" **HERE IS ONE OF THE LINES**
FileClose(FileNum) 'Close the file
FileOpen(FileNum, "enrolments.txt", OpenMode.Input) 'Open the file up again
Do Until I = 5
TempLine = LineInput(FileNum)
TempString += TempLine
txtName.Text = TempString
I += 1
TempString = ""
TempLine = LineInput(FileNum)
TempString += TempLine
txtGender.Text = TempString
TempString = ""
I += 1
TempLine = LineInput(FileNum)
TempString += TempLine
txtAge.Text = TempString
TempString = ""
I += 1
TempLine = LineInput(FileNum)
TempString += TempLine
txtDateOfEnrolment.Text = TempString
TempString = ""
I += 1
TempLine = LineInput(FileNum)
TempString += TempLine
txtPaidUpFront.Text = TempString
TempString = ""
I += 1
Loop
FileClose(FileNum)
CurrentDisplayLine = 5 'Make the CurrentDisplayLine equal "5"
Else
Do Until I = EndLineNum 'Loop this code until I equals the EndLineNum Value **HERE IS THE OTHER LINE**
TempLine = LineInput(FileNum)
TempString += TempLine
txtName.Text = TempString
I += 1
TempString = ""
TempLine = LineInput(FileNum)
TempString += TempLine
txtGender.Text = TempString
TempString = ""
I += 1
TempLine = LineInput(FileNum)
TempString += TempLine
txtAge.Text = TempString
TempString = ""
I += 1
TempLine = LineInput(FileNum)
TempString += TempLine
txtDateOfEnrolment.Text = TempString
TempString = ""
I += 1
TempLine = LineInput(FileNum)
TempString += TempLine
txtPaidUpFront.Text = TempString
TempString = ""
I += 1
Loop
FileClose(FileNum)
End If
CurrentDisplayLine = I
End Sub
End Class
Thanks for taking the time to help me this has bee driving me bonkers! :D
(I should probably mention that I am still pretty new to the whole coding thing so I may not understand how to implement some of your suggestions into my code! Sorry!)