如何使用StreamReader将某些行读入文本框?

时间:2016-02-17 19:22:09

标签: vb.net

我有一份上学的任务,我需要一些帮助。

我制作了一个程序,您可以输入有关员工的信息并将该信息保存到.txt文件中。

之后我创建了一个从.txt文件中读取的程序,并在一些文本框中显示信息。

我的.txt文件中有三名员工。

当我在我的最新程序中按下“下一步”按钮时,我希望它显示下一位员工,依此类推。它只显示有关第一个的信息,而不会显示在列表中的下一个信息。

我该怎么做?

到目前为止我的代码:

Dim EmployeeData As StreamReader


EmployeeData = File.OpenText("C:\Users\Andy\Desktop\Assignment 2-1\Assignment 2-1\bin\Debug\Test123.txt")


txtRecord.Text = (strFileName)
txtFirst.Text = EmployeeData.ReadLine()
txtMiddle.Text = EmployeeData.ReadLine()
txtLast.Text = EmployeeData.ReadLine()
txtID.Text = EmployeeData.ReadLine()
txtDepartment.Text = EmployeeData.ReadLine()
txtTelephone.Text = EmployeeData.ReadLine()
txtExtension.Text = EmployeeData.ReadLine()
txtEmail.Text = EmployeeData.ReadLine()

EmployeeData.Close()

3 个答案:

答案 0 :(得分:1)

您没有按顺序阅读该文件。

您打开一次(EmployeeData = File.OpenText("C:\Users\Andy\Desktop\Assignment 2-1\Assignment 2-1\bin\Debug\Test123.txt"),然后一次读取一行,关闭文件并再次执行该操作。您没有提前移动缓冲区或读取其他内容不止于此。

试试这个(或类似的):

Dim EmployeeData = File.ReadAllLines("PathToFile.txt")
For i As Integer = 0 To 7 Step 8
    txtRecord.Text = (strFileName)
    txtMiddle.Text = EmployeeData(i + 1)
    txtLast.Text = EmployeeData(i + 2)
    txtID.Text = EmployeeData(i + 3)
    txtDepartment.Text = EmployeeData(i + 4)
    txtTelephone.Text = EmployeeData(i + 5)
    txtExtension.Text = EmployeeData(i + 6)
    txtEmail.Text = EmployeeData(i + 7)
Next

这将解决您的直接问题(或者至少它应该,我没有测试它),但不会进一步理解这些概念(如果您是学生)。这里的一些阅读将对文件操作有所帮助:

How to: Read Text from a File
File.ReadAllLines Method (String)
File Methods

答案 1 :(得分:0)

在应用加载时,将员工数据一次读入全局数组,例如:

Public empData() = file.ReadAllLines("C:\Users\Andy\Desktop\Assignment 2-1\Assignment 2-1\bin\Debug\Test123.txt")

同时建立一个计数器,同时也作为全球计数器:

Public empCounter as Integer = 0

然后,在你的btnNext.Click事件中,读取你需要的值并递增计数器:

txtMiddle.Text = empData(empCounter * 7 + 0)
txtLast.Text = empData(empCounter * 7 + 1)
txtID.Text = empData(empCounter * 7 + 2)
txtDepartment.Text = empData(empCounter * 7 + 3)
txtTelephone.Text = empData(empCounter * 7 + 4)
txtExtension.Text = empData(empCounter * 7 + 5)
txtEmail.Text = empData(empCounter * 7 + 6)

对上述内容进行微调,以适合您的文本文件架构。当你到达数据末尾时,还要做一些关于将计数器包装回0或1的事情。

答案 2 :(得分:0)

如果您以CSV格式保存数据(EmployeeData),那么您可以非常轻松地执行此操作.....您的CSV文件将有8条记录,如此...

John,Smith,Doe,10,Development,02081236456,123,JSDoe @Yahoo.co.uk

然后你的代码看起来像这样......

Imports System.Data.OleDb

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim folder = "<Path to the folder of your Test123.txt file (don't include the file name, just the path up to the folder)>"
    Dim CnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folder & ";Extended Properties=""text;HDR=No;FMT=Delimited"";"
    Dim dt As New DataTable
    Using Adp As New OleDbDataAdapter("select * from [Test123.txt]", CnStr)
        Try
            Adp.Fill(dt)
        Catch ex As Exception

        End Try
    End Using
    DataGridView1.DataSource = dt
End Sub
End Class

显示DataGridView中的所有数据....这是你的作业,而且#Sir;&#39;会知道你是否写过它,所以如果你想得到一个A +&#39;,请考虑我的代码,弄清楚发生了什么,然后做其余的事情......