This code gives me an error saying that the index is out of bounds of the array, but if I use MessageBox.Show()
instead of ListBox.Items.Add()
the error doesn't occur.
string[] arr = new string[gradeListbox.Items.Count];
for (int i = 0; i < gradeListbox.Items.Count; i++)
{
arr[i] = gradeListbox.Items[i].ToString();
Regex reg = new Regex(@"[0-9\.]+");
grade = double.Parse(reg.Match(arr[i].ToString()).Value);
studentName = Regex.Replace(arr[i], @"[\d-]", string.Empty);
gradelistbox.Items.Add(grade + studentName);
// ...
}
What is going on? How can I fix it so that it works with a ListBox?