我得到了一個RichTextBox(txt)和一個OpenFileDialog(Open1)。當我嘗試以下方法,我對指定線路IndexOutOfRangeException:IndexOutOfRangeException - RichText
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
If (Open1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
fName = Open1.FileName
txt.LoadFile(fName, RichTextBoxStreamType.PlainText)
Dim i As Integer = 0
While i < txt.Lines.Length
AnalyseLine(i)
i = i + 1
End While
End If
End Sub
Private Sub AnalyseLine(ByVal line As Integer)
Select Case txt.Lines(line).Substring(0, 1) '''' EXCEPTION ON THIS LINE ''''
' [...]
End Select
End Sub
異常拋出每次,不管什麼線變量的價值。 我真的不明白爲什麼,因爲它會在每個文本文件中引發同樣的異常。此外,它似乎像出現此問題,只有當一個新的文件是開放的,因爲用下面的代碼,它工作正常...
Private Sub txt_TextChanged(sender As Object, e As EventArgs) Handles txt.TextChanged
AnalyseLine(txt.GetLineFromCharIndex(txt.GetFirstCharIndexOfCurrentLine))
End Sub
我得到它的工作,通過更換 txt.LoadFile(FNAME,RichTextBoxStreamType.PlainText) 通過 txt.Text = System.IO.File.ReadAllText(FNAME) 但我還是不明白... – Lovy