2017-01-07 117 views
0

我試圖從文本文件中讀取客戶信息。但我似乎只能得到第一個記錄。我假設我的問題來自每次我點擊從文件開始讀取的按鈕。有沒有辦法像做一樣做,並閱讀點擊按鈕上的下一組客戶信息?雖然循環在流讀取器?

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文件:

Stockes 
Colton 
855 
134 Street 
Scottsdale 
az 
85260 
9283806778 
200 
11/2/2017 
Brianne 
Riggle 
1234 
16553 
Scottsdale 
az 
8560 
555555 
110 
11/9/2017 
Gillespie 
Delinda 

7268 Goonight ln 
Prescott 
Az 
86314 
6026848036 
10 
11/20/2017 

回答

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 

enter image description here

+0

真棒謝謝SO 3 M UCH。我正在做類似的事情,並被卡住了。這是一個任務,因爲這正是我正在尋找的! –

+0

@BrianneRiggle請注意我做了一些編輯。 – MrGadget