2017-04-15 109 views
1

目前在从文件中读取数据并将其分配给表单上的文本框时出现问题。代码运行时没有抛出错误,但没有数据/值传递给文本框,并且它们保持空白。下面是我的代码:阅读文件时遇到的问题

Imports System.IO 
Public Class ReadingEmployeeData 
    ' Declare filename 
    Dim FileName As String 
    Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click 
     ' Constant for holding the number of fields 
     Const NumFields As Integer = 8 

     ' Declare variables 
     Dim FileStream As StreamReader 
     Dim Count As Integer 


     Try 
      ' Open file 
      FileStream = File.OpenText(FileName) 

      ' Read the data 
      For Count = 1 To NumFields 
       FirstText.Text = FileStream.ReadLine() 
       MiddleText.Text = FileStream.ReadLine() 
       LastText.Text = FileStream.ReadLine() 
       NumberText.Text = FileStream.ReadLine() 
       DepartmentText.Text = FileStream.ReadLine() 
       PhoneText.Text = FileStream.ReadLine() 
       ExtText.Text = FileStream.ReadLine() 
       EmailText.Text = FileStream.ReadLine() 

      Next Count 

     Catch ex As Exception 
      MessageBox.Show("That file can not be opened.") 
     Finally 
      FileStream.Close() 
     End Try 
    End Sub 

    Private Sub ReadingEmployeeData_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     ' Get the filename from user 
     FileName = InputBox("Please enter the name of the file") 
    End Sub 

    Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click 
     Me.Close() 
    End Sub 
End Class 
+0

显示简单的文本文件中的数据? – Hadi

+0

尝试只使用'Dim FileStream As StreamReader(FileName)'并移除该'FileStream = File.OpenText(FileName)'**更新**更好地查看您的代码,您尝试从文本文件中读取64“行”? – nelek

+0

这是一个调试问题。如果有10名员工,则只显示最后一名员工。我怀疑基于按钮名称,你想每次跳过一些行。 **很多**更好地阅读整个文件('File.ReadAllLines()')并从那里更新UI, – Plutonix

回答

0

使用Io.StreamReader,而且也没有必要NumFields尝试:

Imports System.IO 
Public Class ReadingEmployeeData 
    ' Declare filename 
    Dim FileName As String 
    Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click 


     ' Declare variables 
     Dim FileStream As StreamReader 
     Dim Count As Integer 


     Try 
      ' Open file 
      Using sr As New Io.StreamReader(FileName) 

      ' Read the data 

       FirstText.Text = sr.ReadLine() 
       MiddleText.Text = sr.ReadLine() 
       LastText.Text = sr.ReadLine() 
       NumberText.Text = sr.ReadLine() 
       DepartmentText.Text = sr.ReadLine() 
       PhoneText.Text = sr.ReadLine() 
       ExtText.Text = sr.ReadLine() 
       EmailText.Text = sr.ReadLine() 

       sr.Close() 

      End Using 

     Catch ex As Exception 

      MessageBox.Show("That file can not be opened.") 

     End Try 
    End Sub 

    Private Sub ReadingEmployeeData_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     ' Get the filename from user 
     FileName = InputBox("Please enter the name of the file") 
    End Sub 

    Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click 
     Me.Close() 
    End Sub 
End Class