-1
當我運行此我做了一個假的「Button1的」測試填充他們將成功地填寫文本框的領域。我怎麼會每分鐘解析一下這個文件,當我再次執行時,我會得到如下所示的錯誤。通過將例程「DisplayForm_Load」添加到button1_click,即使它可以正常工作。錯誤與VB ParseFileText方法
我的問題是我很確定我不應該每次重新定義這個?我認爲我並沒有把索引設置回0或者其他方面。從我從MS網站上能夠理解的是,它像它在數組中索引的東西一樣不存在。
錯誤接收: 型 'System.IndexOutOfRangeException' 未處理的異常發生在WindowsApplication4.exe
Imports Microsoft.VisualBasic.FileIO
公共類Form1中
Private Directory As String ' Used to hold the folder directory to push/pull data from.
Private FileParser As Microsoft.VisualBasic.FileIO.TextFieldParser
Private Sub PushButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PushButton.Click
' Sends information to txt file.
' This bit works fine, just writes code to txt file that can be parsed below.
End Sub
Sub DefineTextFieldParse_Load() Handles MyBase.Load
' Instantiate teh TextFieldParser and the set the delimiter
Dim FileName As String = "C:\Users\Caleb\Documents\TestDoc.txt"
Try
FileParser = New FileIO.TextFieldParser(FileName) '' Selects File to Parse
FileParser.TextFieldType = FieldType.Delimited
FileParser.SetDelimiters(",")
Catch ex As Exception
' Errors
MessageBox.Show("Unable to read the file" & "," & FileName)
End Try
End Sub
Sub UpdateCheck()
' Checks share txt file for update.
Dim FileName As String = "C:\Users\<Me>\Documents\TestDoc.txt"
Dim FieldString() As String
'Read the file
If Not FileParser.EndOfData Then
FieldString = FileParser.ReadFields()
' 1st Field
NIS1TextBox.Text = FieldString(0)
' 2nd Field
NIS2TextBox.Text = FieldString(1)
' You get the idea...All Testboxes identified above in the write section
' Repeats 12 more times...
EODTextBox.Text = FieldString(14)
InfoRichTextBox.Text = FieldString.LastOrDefault()
End If
End Sub
Sub PushUpdate()
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
'DefineTextFieldParse_Load()_Load() ' When Enabled code works fine when commented out generates alert.
UpdateCheck()
End Sub
末級
在哪一行執行異常?該行使用了什麼索引?該指數的有效範圍是什麼? – jmcilhinney
我建議你沒有一個'TextFieldParser'對象。你應該在需要的時候和地點創建一個,然後立即銷燬它。您應該使用'Using'語句創建它,然後在塊的末尾隱式銷燬它。當你這樣做時,從最後一次使用中不可能有任何東西被擱置。 – jmcilhinney