我试图从文本文件中读取客户信息。但我似乎只能获得第一张唱片。我假设我的问题来自每次我点击它从文件的开头读取的按钮。有没有办法做一些事情,并在点击按钮上阅读下一组客户信息?
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
searchFile = File.OpenText("Records.txt")
Dim CustomerSearch As CustomerAccounts
Using sr As StreamReader = searchFile
CustomerSearch.LastName = searchFile.ReadLine()
CustomerSearch.FirstName = searchFile.ReadLine()
CustomerSearch.CustomerNumber = searchFile.ReadLine()
CustomerSearch.Address = searchFile.ReadLine()
CustomerSearch.City = searchFile.ReadLine()
CustomerSearch.State = searchFile.ReadLine()
CustomerSearch.ZIPCode = searchFile.ReadLine()
CustomerSearch.TelephoneNumber = searchFile.ReadLine()
CustomerSearch.AccountBalance = searchFile.ReadLine()
CustomerSearch.DateOfLastPayment = searchFile.ReadLine()
txtLast.Text = CustomerSearch.LastName.ToString()
txtFirst.Text = CustomerSearch.FirstName.ToString()
txtNumber.Text = CustomerSearch.CustomerNumber.ToString()
txtAddress.Text = CustomerSearch.Address.ToString()
txtCity.Text = CustomerSearch.City.ToString()
txtState.Text = CustomerSearch.State.ToString()
txtZip.Text = CustomerSearch.ZIPCode.ToString()
txtTelephone.Text = CustomerSearch.TelephoneNumber.ToString()
txtBalance.Text = CustomerSearch.AccountBalance.ToString()
txtPayment.Text = CustomerSearch.DateOfLastPayment.ToString()
End Using
End Sub
这是我的示例txt文件:
ln
Colton
855
134 Street
Scottsdale
az
85260
1234567894
200
11/2/2017
Brianne
ln
1234
16553
Scottsdale
az
8560
1234567894
110
11/9/2017
ln
Delinda
0123
123 Goonight ln
Prescott
Az
86314
1234567894
10
11/20/2017
答案 0 :(得分:1)
那里有一些丑陋的数据,但是仍然......我在一个表格上放了3个按钮(btnLoad,btnPrevious和btnNext)和10个文本框,模拟你的类,这在你的表单代码中:
Imports System.IO
Public Class Form1
Private CustomerSearch As New List(Of CustomerAccounts)
Private CurrentCust As Integer = 0
Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
With File.OpenText("Records.txt")
While Not .EndOfStream
' This is fragile if the data lines don't match perfectly to fields and line count
Dim NewCust As New CustomerAccounts
NewCust.LastName = .ReadLine
NewCust.FirstName = .ReadLine
NewCust.CustomerNumber = .ReadLine
NewCust.Address = .ReadLine
NewCust.City = .ReadLine
NewCust.State = .ReadLine
NewCust.ZIPCode = .ReadLine
NewCust.TelephoneNumber = .ReadLine
NewCust.AccountBalance = .ReadLine
NewCust.DateOfLastPayment = .ReadLine
CustomerSearch.Add(NewCust)
End While
.Dispose()
End With
Debug.Print("Loaded {0} Records.", CustomerSearch.Count)
If CustomerSearch.Count > 0 Then ShowData()
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
If CurrentCust > 0 Then
CurrentCust -= 1
ShowData()
Else
Debug.Print("Already at first record")
End If
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
If CurrentCust < CustomerSearch.Count - 1 Then
CurrentCust += 1
ShowData()
Else
Debug.Print("Already at last record")
End If
End Sub
Sub ShowData()
Debug.Print("Loading record {0}", CurrentCust.ToString)
With CustomerSearch(CurrentCust)
txtLast.Text = .LastName.ToString
txtFirst.Text = .FirstName.ToString
txtNumber.Text = .CustomerNumber.ToString
txtAddress.Text = .Address.ToString
txtCity.Text = .City.ToString
txtState.Text = .State.ToString
txtZip.Text = .ZIPCode.ToString
txtTelephone.Text = .TelephoneNumber.ToString
txtBalance.Text = .AccountBalance.ToString
txtPayment.Text = .DateOfLastPayment.ToString
End With
End Sub
End Class
Class CustomerAccounts
Property LastName As String
Property FirstName As String
Property CustomerNumber As String
Property Address As String
Property City As String
Property State As String
Property ZIPCode As String
Property TelephoneNumber As String
Property AccountBalance As String
Property DateOfLastPayment As String
End Class