'StartIndex不能小于零。当试图在vb中加载表单时

时间:2012-09-06 11:36:12

标签: vb.net visual-studio-2005

我最近对我的vb项目中的一个sub进行了一些更改,现在我在尝试加载表单时遇到错误。我不清楚start indexParameter name与表单的关系,所以不要从哪里开始寻找解决此问题的方法。这是我收到的错误消息:

An error occurred creating the form. See Exception.InnerException for details.  The error is: StartIndex cannot be less than zero.
Parameter name: startIndex

我更改的Sub是下面代码中名为TextBox1changed_textchanged的最后一个子。我添加了除最后5行之外的所有行,以限制可以放入文本框的字符。这个新代码是从另一个论坛页面编辑的,所以我认为它应该可以正常工作,但我无法确定该表格将不再运行。

Public Class frmAddQuantity

Private Sub frmFieldMaster_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    cmbStateRateSumRatio.Items.Clear()
    cmbStateRateSumRatio.Items.Insert(0, "State")
    cmbStateRateSumRatio.Items.Insert(1, "Rate")
    cmbStateRateSumRatio.Items.Insert(2, "Sum")
    cmbStateRateSumRatio.Items.Insert(3, "Ratio")


End Sub

Private Sub bttAddQUAtoDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttAddQUAtoDatabase.Click

    AddQuantity("'" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & cmbStateRateSumRatio.Text & "'")

    InitialiseAll()
    frmFieldMaster.InitialiseNewParameter()
    Me.Close()

End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

    If TextBox2.Text <> "Enter SI Units" Then
        If cmbStateRateSumRatio.SelectedIndex <> -1 Then
            bttAddQUAtoDatabase.Enabled = True
        End If
    End If
End Sub

Private Sub cmbStateRateSumRatio_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbStateRateSumRatio.SelectedIndexChanged

    If TextBox1.Text <> "Enter Quantity" Then
        If TextBox2.Text <> "Enter SI Units" Then
            bttAddQUAtoDatabase.Enabled = True
        End If
    End If

End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged


    Dim charactersAllowed As String = " abcdefghijklmnopqrstuvwxyz1234567890^-*()."
    Dim Text As String = TextBox2.Text
    Dim Letter As String
    Dim SelectionIndex As Integer = TextBox2.SelectionStart
    Dim Change As Integer




    Letter = TextBox2.Text.Substring(SelectionIndex - 1, 1)
    If Letter = "/" Then
        Text = Text.Replace(Letter, "^(-")
        SelectionIndex = SelectionIndex - 1
    End If



    Letter = TextBox2.Text.Substring(SelectionIndex - 1, 1)
    If charactersAllowed.Contains(Letter) = False Then
        Text = Text.Replace(Letter, String.Empty)
        Change = 1
    End If


    TextBox2.Text = Text
    TextBox2.Select(SelectionIndex - Change, 0)

    If TextBox1.Text <> "Enter Quantity" Then
        If cmbStateRateSumRatio.SelectedIndex <> -1 Then
            bttAddQUAtoDatabase.Enabled = True
        End If
    End If

End Sub

结束班

2 个答案:

答案 0 :(得分:1)

告诉调试器在抛出异常时停止。 Debug + Exceptions,勾选CLR例外的Thrown复选框。

您的代码段中有一个很好的候选人可以解决此异常:

Letter = TextBox1.Text.Substring(SelectionIndex - 1, 1)

您需要处理文本框为空或者SelectionStart属性为0的可能性。这将始终使用“StartIndex不能小于零”轰炸您的代码。

答案 1 :(得分:0)

Private Const AllowedChars = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890^-*()."

Private Sub TxtSIUnit_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSIUnit.KeyPress
    If e.KeyChar >= " "c AndAlso Not AllowedChars.Contains(e.KeyChar) Then e.Handled = True
End Sub

Private Sub TxtSIUnit_KeyPress2(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSIUnit.KeyPress
    Dim selectstart
    If e.KeyChar = "/" Then
        e.Handled = True
        selectstart = txtSIUnit.SelectionStart
        txtSIUnit.SelectedText = "*^-1"
        txtSIUnit.Select(selectstart + 1, 0)
    End If
End Sub

我已使用此代码解决了上述问题。我没有使用.textchanged句柄,而是使用.keypress来阻止我以前遇到过的问题。文本框中的文本在加载表单时发生了变化,导致文本的起始索引出现问题。使用.keypress意味着只有用户输入才能运行代码,避免加载时出错。