索引超出了数组.VB的范围



我一直在尝试创建一个程序,该程序可以在多行文本框中找到用户单击的单词。此过程基于单击位置的索引。我实现的代码:

Public Class Form1
Private Sub TextBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles TextBox1.MouseDown
If e.Clicks = 1 And e.Button = MouseButtons.Left Then
'Try
Dim indexClicked As Integer = TextBox1.GetCharIndexFromPosition(New Point(e.X, e.Y))
Dim ch As Char = TextBox1.Text.Chars(indexClicked)
Dim indexOfWord As Int32
If Not ch = " " Then
Dim wordFound As Boolean
Dim previousCh As Char
Dim previousIndex As Integer = indexClicked
While Not wordFound
previousIndex = previousIndex - 1
previousCh = TextBox1.Text.Chars(previousIndex)
If previousCh = " " Then
indexOfWord = previousIndex + 1
wordFound = True
End If
End While
Else
indexOfWord = indexClicked + 1
End If
Label1.Text = indexClicked & ", " & indexOfWord
Label2.Text = GetWordByIndex(TextBox1.Text, indexOfWord)
'  Catch ex As Exception
'  Label2.Text = ex.Message
' End Try
End If
End Sub
Public Shared Function GetWordByIndex(input As String, index As Integer) As String
Try
Dim words = input.Split(" ")
If (index < 0) OrElse (index > words.Length - 1) Then
Throw New IndexOutOfRangeException("Index out of range!")
End If
Return words(index)
Catch ex As Exception
'handle the exception your way
Return String.Empty
End Try
End Function
End Class

问题是,每当程序到达行时:

previousCh = TextBox1.Text.Chars(previousIndex)

它以 :

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in WindowsApplication1.exe
Additional information: Index was outside the bounds of the array.

当抛出异常时,通过将鼠标悬停在上一个 Index 变量上,Visual Studio 向我显示其值:-1。

我认为previousCh = " "条件永远不会成立,因此程序永远不会退出 while 循环,该循环会不断寻找前一个字符。在某些时候,int previousIndex 变为负数,程序崩溃。为什么条件不能正常工作?

问题出在哪里? 谢谢。

如果您不想像David Wilson建议的那样让用户双击(我也同意(,那么这将得到您想要的结果。它考虑了前一个字符是换行符还是文本的开头,或者下一个字符是换行符还是文本的结尾。如果需要,您可以添加到If以查找","或"."。

Private Sub TextBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles TextBox1.MouseDown
If e.Clicks = 1 And e.Button = MouseButtons.Left Then
Dim startIndex As Integer = TextBox1.SelectionStart
Dim wordStartFound, wordEndFound As Boolean
Dim nextIndex, indexOfStartOfWord, indexOfEndOfWord, lengthOfWord As Integer
If Not startIndex = 0 Then
While Not wordStartFound
startIndex = startIndex - 1
If TextBox1.Text.Chars(startIndex) = " " Then
indexOfStartOfWord = startIndex + 1
wordStartFound = True
ElseIf startIndex = 0 Then
indexOfStartOfWord = startIndex
wordStartFound = True
ElseIf TextBox1.Text.Chars(startIndex) = Chr(10) Then 'Line Feed' 
indexOfStartOfWord = startIndex + 1
wordStartFound = True
End If
End While
Else
indexOfStartOfWord = startIndex
End If
nextIndex = startIndex
While Not wordEndFound
nextIndex = nextIndex + 1
If TextBox1.Text.Chars(nextIndex) = " " Then
indexOfEndOfWord = nextIndex
wordEndFound = True
ElseIf nextIndex = TextBox1.TextLength - 1 Then
indexOfEndOfWord = TextBox1.TextLength
wordEndFound = True
ElseIf TextBox1.Text.Chars(nextIndex) = Chr(10) Then 'Line Feed' 
indexOfEndOfWord = nextIndex
wordEndFound = True
End If
End While
lengthOfWord = indexOfEndOfWord - indexOfStartOfWord
Label2.Text = TextBox1.Text.Substring(indexOfStartOfWord, lengthOfWord)
End If
End Sub

同样在您的函数GetWordByIndex中,您将输入字符串拆分为一个数组

Dim words = input.Split(" ")

然后你说

If (index < 0) OrElse (index > words.Length - 1) Then Throw New IndexOutOfRangeException("Index out of range!") End If

但是当你在数组上调用.length时,它会返回字符串的数量(或数组中的任何内容(,例如,如果输入是"棕色的大狐狸跳过懒狗",words.length - 1将返回 8。因此,如果您传递的索引是单词"over"的开头,它将落入Throw New IndexOutOfRangeException("Index out of range!")因为索引将是 26,这显然大于 8。

我提供的代码没有使用该函数来查找单词,但我想无论如何我都会提到这一点。

最新更新