0

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

Dark Bolt
  • 3
  • 2
  • 4
    You shouldn't be using `FileOpen` and all those garbage VB6 holders in the first place. Use the `System.IO` namespace for working with files. You can create a `StreamReader` in various ways and its `EndOfStream` property will tell you when you're at the end. You can either close it or seek back to the beginning of the underlying `FileStream`. – user18387401 May 02 '22 at 12:10
  • 4
    You can read the entire file into a string array with one line of code: `Dim lines As String() = File.ReadAllLines("enrolments.txt")`. Then you can get the number of lines with `lines.Length`. You can read this array many times without having to reopen the file. – Olivier Jacot-Descombes May 02 '22 at 12:25
  • Maybe you should use a more appropriate file type. See: [trying to serialize and deserialize an xml file using VB.net](https://stackoverflow.com/q/24246002/880990) – Olivier Jacot-Descombes May 02 '22 at 12:48
  • @Olivier Jacot-Descombes. I have entered your code into the file but the "File" in the code line: "Dim lines As String() = File.ReadAllLines("enrolments.txt")" is apparently not declared or may be inaccessible due to it's protection level? How do I fix this? – Dark Bolt May 04 '22 at 13:32
  • Import namespace System.IO. Visual Studio will show you a smart tag that will do it for you. – Olivier Jacot-Descombes May 05 '22 at 13:13

0 Answers0