VB中出错:language.exe中出现未处理的“System.IndexOutOfRangeException”类型异常

时间:2014-01-19 21:08:00

标签: vb.net

language.exe中发生未处理的“System.IndexOutOfRangeException”类型异常

其他信息:索引超出了数组的范围。

这里是错误的代码:

Private Sub RichTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles         RichTextBox1.KeyDown
    Dim i As Integer = 0
    If e.KeyCode = Keys.Enter Then
        i = i + 1
        Dim t As String = RichTextBox1.Lines(i).ToString()
        ListBox1.Items.Add(t)
    End If
End Sub

1 个答案:

答案 0 :(得分:3)

试试这个

If e.KeyCode = Keys.Enter Then
    Dim index As Integer = RichTextBox1.SelectionStart
    Dim line As Integer = RichTextBox1.GetLineFromCharIndex(index)
    ListBox1.Items.Add(RichTextBox1.Lines(line).ToString())
End If