bluanwht 0 Newbie Poster

Here is the code I have. I am trying to collect five scores and then print each score followed by the average score. Seems like it would be simple but I am having problems. Any suggestions?

Option Strict On
Public Class frmScore
   Private Sub frmScore_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      lstResult.Items.Clear()
   End Sub

   Private Sub txtScore_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtScore.KeyPress
      Dim KeyAscii As Integer = Asc(e.KeyChar)
      If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 And KeyAscii <> 13 Then
         KeyAscii = 0
      End If
      If KeyAscii = 0 Then
         e.Handled = True
      End If
   End Sub

   Private Sub btnScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
      Dim score As Integer = 0
      If txtScore.Text = "" Then
         MessageBox.Show("Please enter a score. ")
         txtScore.Focus()
      Else
         score = Convert.ToInt32(txtScore.Text)
         lstResult.Items.Add(score)
         txtScore.Text = ""
         txtScore.Focus()
         If lstResult.Items.Count = 5 Then
            btnAdd.Enabled = False
            btnCalculate.Enabled = True
         End If
      End If

   End Sub

   Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
      Dim total As Integer = 0
      Dim average As Integer = 0
      For counter As Integer = 0 To 4
         total += Convert.ToInt32(lstResult.Items(counter))
         average = total / counter
      Next
      lstResult.Items.Add("Average score is: " & average)
   End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.