0

I have designed a form to open a .txt file, read the values and display them into the forms label controls. My problem is that I cant figure out how to advance to read the next record when I click on the next record btn. I am able to read the first 9 lines that are written to the file, but nothing after that. I need to be able to read lines 10 thru 19 and display them in the forms labels when the btnNext is clicked, Then display the next set of values lines 20 thru 29 when btnNext is clicked again. I think I need to figure out a way to increment the record count and reader.

Below is a sample of the .txt file I am opening as well as the forms code.

Any help or suggestions would be greatly appreciated. Thanks

Example of file lay out:

Record Number
First Name
Middle Name
Last Name
Employee Number
Dept
Phone
Extension
Email
__________

File Values
_________
462
Mike
B
Davis
1122
Accounting
322-0009
112
mike@aol.com
612
Sam
L
Jackson
1048
Administration
966-7589
2
sam@aol.com
398
Lin
P
Daniels
1014
Marketing
966-2217
5
Lin@aol.com
___________

Code Example:

Option Strict On
Imports System.IO

Public Class frmMain
    Inherits System.Windows.Forms.Form


    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' Let the user select a file to open. Pass the selected file to the ReadFile procedure.
        With ofdOpenFile
            .Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
            .Title = "Select a File to Open"
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                If .FileName <> String.Empty Then
                    ReadFile(.FileName)
                Else
                    MessageBox.Show("No file selected.", "Error")
                End If
            End If
        End With


    End Sub

    Sub ReadFile(ByVal strFileName As String)

        ' Read the contents of the specified file into the UnitsSold array.

        Dim inputFile As StreamReader
        Dim intCount As Integer = 1

        ' Declare variables.
        Dim intN As Integer
        Dim strFirst As String
        Dim strMiddle As String
        Dim strLast As String
        Dim intEmpNum As Integer
        Dim strDept As String
        Dim strPhone As String
        Dim intExt As Integer
        Dim strEmail As String

        ' Open the file. 
        inputFile = File.OpenText(strFileName)

        ' Read the data.
        For intCount = 1 To 1
            ' Read the record from the file.
            intN = CInt(inputFile.ReadLine())
            strFirst = inputFile.ReadLine()
            strMiddle = inputFile.ReadLine()
            strLast = inputFile.ReadLine()
            intEmpNum = CInt(inputFile.ReadLine())
            strDept = inputFile.ReadLine()
            strPhone = inputFile.ReadLine()
            intExt = CInt(inputFile.ReadLine())
            strEmail = inputFile.ReadLine()

            ' Display the input data on the record form.

            lblN.Text = intN.ToString()
            lblFirst.Text = strFirst.ToString()
            lblMiddle.Text = strMiddle.ToString()
            lblLast.Text = strLast.ToString()
            lblEmpNum.Text = intEmpNum.ToString()
            lblDept.Text = strDept.ToString()
            lblPhone.Text = strPhone.ToString()
            lblExt.Text = intExt.ToString()
            lblEmail.Text = strEmail.ToString()
        Next intCount

        ' Close the file. 
        inputFile.Close()

    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click

        ' Here is where I need some help!

    End Sub
End Class
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by waynespangler
0

The reason is because in your ReadFile you are opening the file, reading a set of data and then closing the file. The next time you use it, it is reading the same record.
Move the Input.Close() to a button to close it and also in FormClosing. However, this is only going to let you go one way in the file, top to bottom, and you will not be able to make changes to the file.
Why not use a database or CSV?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.