Katie B

Katie B

  • NA
  • 66
  • 18.1k

C# - Input string was not in correct format!

Apr 14 2013 1:07 PM
Evening All, 

I'm trying to get this bit of code to take the numbers from the list box and add them in the label named ucaspoints. But when I write this code: 

private void btnAdd_Click(object sender, EventArgs e)
        {
            int totalpoints = 0;
            

            string item = lstboxGrades.SelectedItem.ToString();
            
            lstboxeducation.Items.Add(item);
            totalpoints = int.Parse(lstboxeducation.Text) + totalpoints;
            ucaspoints.Text = totalpoints.ToString();
          
           
             
        }

It runs without error, but only displays the numbers in the list box rather than adding them. Ive tried adapting this to:
 private void btnAdd_Click(object sender, EventArgs e)
        {
            int totalpoints = 0;
            

            string item = lstboxGrades.SelectedItem.ToString();
            
            lstboxeducation.Items.Add(item);
            totalpoints = Convert.ToInt32(lstboxeducation.Text) + totalpoints;
            ucaspoints.Text = totalpoints.ToString();
          
           
             
        }
 
Swapping the int.Parse to Convert.ToInt32 but then i receive a run time error stating that the "Input string is not in the correct format" (on the line in bold). Ive tried putting break points in and with the int.Parse it runs through it no problem, but it will stop at the convert.toint32 and give the above error. 

Any advice would be great. Thanks

Answers (2)