【问题标题】:Index was outside the bounds of the array. VB指数数组的边界之外。 VB
【发布时间】:2018-07-02 11:54:46
【问题描述】:

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

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.

当抛出异常时,通过将鼠标悬停在 previousIndex 变量上,visual studio 会显示它的值:-1。

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

有什么问题? 谢谢。

【问题讨论】:

  • 追踪 -1 的来源。我想它应该是0或1。是因为减法导致了这个吗?是不是在某处找不到的默认返回值?
  • 这是有用的信息,即使是假设,也可以edit 进入问题。
  • 让用户双击单词并使用.SelectionChanged.SelectionStart.SelectionLength.SelectedText属性不是更容易吗?

标签: vb.net winforms


【解决方案1】:

如果您不想像 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。

我提供的代码没有使用该函数来查找单词,但我想我还是会提到这一点。

【讨论】:

    猜你喜欢
    • 2011-04-15
    • 2018-10-26
    • 1970-01-01
    相关资源
    最近更新 更多