Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8789937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:31:55+00:00 2026-06-13T22:31:55+00:00

I have recently made some changes to one of the subs within a form

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:31:55+00:00Added an answer on June 13, 2026 at 10:31 pm
    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
    

    I have solved the issue above using this code. Instead of using the .textchanged handle I have used .keypress which 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 .keypress mean only user inputs let the code run, avoiding errors on loading.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently made changes in some code that makes a char name field
I have recently made some changes to my hosting account and a script I
I have recently merged together 5 of my stand-alone projects into one project to
I released an update to one of my apps recently. This update made some
I recently made some changes to my development environment and came across a problem.
I recently made some changes to a working publication under Merge replication which seem
I have started making some changes recently to my master branch to add featureX.
I have an iPhone app that is using CoreData. I recently made some minor
Hi I have recently made this script to rename files I scan for work
I grew up on Java and have recently made a full switch to C#.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.