I have recently made some changes to one of the subs within a form of my vb project and I am now getting an error whilst attempting to load the form. I have little idea what start index or Parameter name are with relation to a form, so don’t where to start looking to solve this issue. Here is the error message I get:
An error occurred creating the form. See Exception.InnerException for details. The error is: StartIndex cannot be less than zero.
Parameter name: startIndex
The Sub that I have made changes to is the last sub in the code below called TextBox1changed_textchanged. I have added all but the last 5 lines as to limit the characters that can be put into the text box. This new code is edited from another forum page so I assume it should work correctly, but I can’t be sure as the form will no longer run.
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
End Class
I have solved the issue above using this code. Instead of using the
.textchangedhandle I have used.keypresswhich stopped the issue I was having previously. The text in the textbox was changing as the form was loading, leading to a problem with the start index of the text. Using.keypressmean only user inputs let the code run, avoiding errors on loading.