Search code examples
vb.netstringcomboboxargumentexception

Form closes error: ArgumentException was unhandled when using Strings in VB.net


There is a weird error when I try to close my Windows Form... I am trying to do a string manipulation of this string: ABEL, SCOTT - 1472 (COL) when the ComboBox Selected Item changed. It is able to work as you can see from the debugger below, but when the form closes, an error throws.

My ComboBox items is populated by a Data Source with the Me.GetRunnersTableAdapter.Fill(Me.WSCDbDataSet.GetRunners) function.

Is this a bug?

enter image description here


Solution

  • I've gotten it to work by using Try, Catch.

    Dim myLastName
    myLastName = InStr(1, cbRunner.Text, ",")
    
    Dim myFirstName
    myFirstName = InStr(myLastName + 2, cbRunner.Text, "-")
    
    Dim myBibNumber
    myBibNumber = InStr(myFirstName + 2, cbRunner.Text, "(")
    
    
    Try
    LastName.Text = Trim$(Strings.Left(cbRunner.Text, myLastName - 1))
    FirstName.Text = Trim$(Mid(cbRunner.Text, myLastName + 2, myFirstName - myLastName - 2))
    BibNumber.Text = Trim$(Mid(cbRunner.Text, myFirstName + 2, myBibNumber - myFirstName - 2))
    Catch ex As ArgumentException
    
    End Try