使用结构、数组、循环和DataGridView写的分数统计小程序

界面如下:

使用结构、数组、循环和DataGridView写的分数统计小程序

代码如下:

Public Class Form1

Const sMax As Integer = 100

Structure StudentType

Dim strID As String

Dim strName As String

Dim sngScore As Single

End Structure

Dim udtStudent(sMax) As StudentType

Dim intCount As Integer = 0

Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click

dgvScore.Rows.Add(txtID.Text, txtName.Text, txtScore.Text)

txtID.Focus()

udtStudent(intCount).strID = txtID.Text

udtStudent(intCount).strName = txtName.Text

udtStudent(intCount).sngScore = CSng(txtScore.Text)

intCount = intCount + 1

txtID.Text = ""

txtName.Text = ""

txtScore.Text = ""

End Sub

Private Sub btnSum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSum.Click

Dim intPos As Integer

Dim intSum As Integer

Dim dgvAvg As Double

intSum = 0

For intPos = 0 To intCount - 1

intSum = intSum + udtStudent(intPos).sngScore

Next

If intCount > 0 Then dgvAvg = intSum / intCount

MsgBox("平均分数:" + dgvAvg.ToString)

txtID.Focus()

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

End

End Sub

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

If Me.dgvScore.SelectedRows.Count > 0 AndAlso _

Not Me.dgvScore.SelectedRows(0).Index = _

Me.dgvScore.Rows.Count - 1 Then _

Me.dgvScore.Rows.RemoveAt(Me.dgvScore.SelectedRows(0).Index)

End Sub

End Class