Question 'System.IndexOutOfRangeException' occurred while getting text from listbox

Jack_NL

New member
Joined
Sep 3, 2014
Messages
2
Location
Netherlands
Programming Experience
3-5
Hi all,

I making a pool system, people can win things. It works fine, but after 3 clicks on the Random button i get a 'System.IndexOutOfRangeException' error.

VB.NET:
Private Sub Random_Click(sender As Object, e As EventArgs) Handles Random.Click
UserTxt.Text = String.Empty
EmailTxt.Text = String.Empty

        Dim r As New Random
        
        RandomTxt.Text = ListBox1.GetItemText(ListBox1.Items(r.Next(0, ListBox1.Items.Count)))

        Dim s As String

        Dim sp() As String
        Dim substring As String

        s = RandomTxt.Text
        sp = s.Split(":")

        UserTxt.Text = sp(0)
        EmailTxt.Text = sp(1)
End Sub

First few times it works fine, then suddenly it get this error. It happens at the EmailTxt.Text = sp(1) part. Am i doing something wrong?
 
Use debugger and look at local variables s/sp, in this case there is no ":" in text so there is only one item in sp array (which is equal to s).
 
Back
Top