-1

I’m trying to create a solution that will pull employee information from a text file, load it to an array, and then have a series of TextBoxes populated from the array when a button is clicked.

I’ve made some progress to where I’m pulling data from the array, and getting it into the text boxes. However, it’s only individual letters populating the TextBoxes, not the full data.

I’ve attached the code I’ve been working on to achieve my goal, but I’m really hitting a block on where to go next.

For reference the data from the text file is:

Janice
Jones
101
5.25
Chris
O Connel
102
5.35
Karen
Fisk
103
6.00
Tom
Winn
104
5.75

Private Sub nextButton_Click(sender As Object, e As EventArgs) Handles nextButton.Click
    Dim EmpoyeeStreamReader As New StreamReader("employee.txt")
    Dim EmployeeLineInfoString As String
    Dim EmployeeIndex As Decimal

    EmployeeLineInfoString = EmpoyeeStreamReader.ReadLine()

    Dim EmployeeArray = (EmployeeLineInfoString).ToString

    EmployeeIndex = 0

    Try
        For EmployeeIndex = 0 To 16
            firstNameTextBox.Text = EmployeeArray(EmployeeIndex)
            EmployeeIndex += 1
            lastNameTextBox.Text = EmployeeArray(EmployeeIndex)
            EmployeeIndex += 1
            employeeNumberTextBox.Text = EmployeeArray(EmployeeIndex)
            EmployeeIndex += 1
            payRateTextBox.Text = EmployeeArray(EmployeeIndex)
        Next

        For EmployeeIndex = 5 To 16
            firstNameTextBox.Text = EmployeeArray(EmployeeIndex)
            EmployeeIndex += 1
            lastNameTextBox.Text = EmployeeArray(EmployeeIndex)
            EmployeeIndex += 1
            employeeNumberTextBox.Text = EmployeeArray(EmployeeIndex)
            EmployeeIndex += 1
            payRateTextBox.Text = EmployeeArray(EmployeeIndex)
        Next
    Catch ex As Exception
        MessageBox.Show("All Employee's have been entered")
    End Try
Blackwood
  • 4,504
  • 16
  • 32
  • 41
DSQLJ
  • 1
  • 3
  • If you read a line into `EmployeeLineInfoString` it is still just a line in `EmployeeArray` no matter what you call it. But since a string is an array of characters, `EmployeeArray(n)` is just a letter. – Ňɏssa Pøngjǣrdenlarp Jun 07 '17 at 01:00
  • So then how would I go about getting the entire line, not just a letter? – DSQLJ Jun 07 '17 at 21:47
  • You already have the entire line in `EmployeeLineInfoString`. The best thing would be to read all the lines into an array, then cycle thru *that* when they click next – Ňɏssa Pøngjǣrdenlarp Jun 07 '17 at 21:51
  • Am I not doing that with Dim EmployeeArray = (EmployeeLineInfoString).ToString? – DSQLJ Jun 07 '17 at 21:55
  • That doesnt do what you think it does. set a breakpoint and hold the mouse over `EmployeeArray` after it executes - it isnt an array. By *read all lines* I meant read all lines of the file into an array at once. The cleverly named `FileReadAllLines` does just that – Ňɏssa Pøngjǣrdenlarp Jun 07 '17 at 22:00
  • So, something like this? Dim EmployeeArray() As String = File.ReadAllLines("employee.txt") – DSQLJ Jun 07 '17 at 22:09
  • Yes but **declare** the array as a form level variable `Private Employees As String()`, then just fill it up with the method: `Employees = File.ReadAllLines("employee.txt")`. You will need another form level variable to keep track of where you are in the list so the next button works. Best to include a path with the file name if you want it to run on a machine other than yours – Ňɏssa Pøngjǣrdenlarp Jun 07 '17 at 22:12
  • So when you say Private do you mean a public class? – DSQLJ Jun 07 '17 at 22:31
  • Please see [Reference variables and objects elsewhere in a form](http://stackoverflow.com/a/33249045/1070452) also please read [ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Jun 07 '17 at 22:34
  • I've got the data to populate into the text boxes. I can start working on making it cycle through the whole list now. Thank you for your time and patientce. – DSQLJ Jun 07 '17 at 22:50

1 Answers1

0

Try something like this out (though it might be better to declare your StreamReader at Form level!):

Private Sub nextButton_Click(sender As Object, e As EventArgs) Handles nextButton.Click
    Static EmpoyeeStreamReader As StreamReader = Nothing

    If IsNothing(EmpoyeeStreamReader) Then
        EmpoyeeStreamReader = New StreamReader("employee.txt")
    End If

    If Not EmpoyeeStreamReader.EndOfStream Then
        Try
            firstNameTextBox.Text = EmpoyeeStreamReader.ReadLine
            lastNameTextBox.Text = EmpoyeeStreamReader.ReadLine
            employeeNumberTextBox.Text = EmpoyeeStreamReader.ReadLine
            payRateTextBox.Text = EmpoyeeStreamReader.ReadLine
        Catch ex As Exception
            MessageBox.Show("Error Reading Record: Insufficient Number of Lines!")
        End Try
    Else
        MessageBox.Show("No more records!")
        EmpoyeeStreamReader.Close()
        EmpoyeeStreamReader = Nothing
    End If
End Sub
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40