tgs266
tgs266

Reputation: 188

Error in VB: An unhandled exception of type 'System.IndexOutOfRangeException' occurred in language.exe

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in language.exe

Additional information: Index was outside the bounds of the array.

here is the code where the error is:

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

Upvotes: 0

Views: 4026

Answers (1)

NetInfo
NetInfo

Reputation: 597

Try this

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

Upvotes: 3

Related Questions