i was try to solve it , but im begnner in vb.net

this is my code

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As New ListViewItem(t1.Text)
        x.SubItems.Add(t2.Text)
        x.SubItems.Add(t3.Text)
        x.SubItems.Add(CInt(t2.Text) + CInt(t3.Text))
        l1.Items.Add(x)
        t1.Text = ""
        t2.Text = ""
        t3.Text = ""

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If l1.SelectedIndices.Count > 0 Then
            For i As Integer = 0 To l1.SelectedIndices.Count - 1
                l1.Items.RemoveAt(l1.SelectedIndices(0))
            Next
        End If
    End Sub
    Dim avg As Double
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim sum As Integer
        For i As Integer = 0 To l1.Items.Count - 1
            sum += l1.Items(i).SubItems(3).Text
        Next
        avg = sum / l1.Items.Count
        t4.Text = avg
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        For i As Integer = 0 To l1.Items.Count - 1
            If CDbl(l1.Items(i).SubItems(3).Text) >= CDbl(avg + 30) Then
                l1.Items(i).SubItems(4).Text = "A+"
            ElseIf CDbl(l1.Items(i).SubItems(3).Text) >= CDbl(avg + 25) Then
                l1.Items(i).SubItems(4).Text = "A"
            ElseIf CDbl(l1.Items(i).SubItems(3).Text) >= CDbl(avg + 20) Then
                l1.Items(i).SubItems(4).Text = "A-"
            ElseIf CDbl(l1.Items(i).SubItems(3).Text) >= CDbl(avg + 15) Then
                l1.Items(i).SubItems(4).Text = "B+"
            ElseIf CDbl(l1.Items(i).SubItems(3).Text) >= CDbl(avg + 10) Then
                l1.Items(i).SubItems(4).Text = "B"
            ElseIf CDbl(l1.Items(i).SubItems(3).Text) >= CDbl(avg + 5) Then
                l1.Items(i).SubItems(4).Text = "B-"
            ElseIf CDbl(l1.Items(i).SubItems(3).Text) >= CDbl(avg + 0) Then
                l1.Items(i).SubItems(4).Text
            ElseIf CDbl(l1.Items(i).SubItems(3).Text) >= CDbl(avg - 5) Then
                l1.Items(i).SubItems(4).Text = "C"
            ElseIf CDbl(l1.Items(i).SubItems(3).Text) >= CDbl(avg - 10) Then
                l1.Items(i).SubItems(4).Text = "C-"
            ElseIf CDbl(l1.Items(i).SubItems(3).Text) >= CDbl(avg - 15) Then
                l1.Items(i).SubItems(4).Text = "D+"
            ElseIf CDbl(l1.Items(i).SubItems(3).Text) >= CDbl(avg - 20) Then
                l1.Items(i).SubItems(4).Text = "D"
            ElseIf CDbl(l1.Items(i).SubItems(3).Text) < CDbl(avg - 20) Then
                l1.Items(i).SubItems(4).Text = "F"
            End If
        Next
    End Sub
End Class

Dani AI

Generated

As noted, how marks are stored and validated is the root of many runtime problems. As pointed out, descriptive control names and a short description of what each button does make diagnosis much faster. As suggested, stepping through the code with the debugger will reveal where conversions or index errors happen.

Key issues to address:

  • Numeric parsing is fragile (CInt/CStr on unchecked text throws exceptions). Use TryParse and trim input.
  • Sum/average types should be Double to avoid integer division and rounding surprises.
  • Division by zero must be guarded (no items => no average).
  • The grade cell can be missing; ensure a placeholder subitem exists before writing to it.
  • Removing selected items should iterate SelectedIndices in reverse to avoid shifting indexes.
  • One grade branch in the posted logic does nothing (missing assignment).

Concrete checklist and quick fixes:

  • Give controls meaningful names (txtName, txtMark1, txtMark2, txtAvg, lvStudents, btnAdd, btnDelete, btnAvg, btnGrade).
  • Validate marks with Integer.TryParse and show a short message when parsing fails.
  • When creating a ListViewItem, add a blank subitem for the grade so SubItems(4) will always exist.
  • Use Double for running sum and avg; check lvStudents.Items.Count > 0 before dividing.
  • Remove selected items in reverse: For i = lvStudents.SelectedIndices.Count - 1 To 0 Step -1 ... RemoveAt(...).

Example (compact) implementations for Add, CalculateAvg and AssignGrade:

' Add
Dim m1 As Integer, m2 As Integer
If Not Integer.TryParse(txtMark1.Text.Trim(), m1) OrElse Not Integer.TryParse(txtMark2.Text.Trim(), m2) Then Return
Dim item As New ListViewItem(txtName.Text.Trim())
item.SubItems.Add(m1.ToString()) : item.SubItems.Add(m2.ToString())
item.SubItems.Add((m1 + m2).ToString()) : item.SubItems.Add(String.Empty)
lvStudents.Items.Add(item)

' Average
If lvStudents.Items.Count = 0 Then txtAvg.Text = "0" : Return
Dim sum As Double = 0
For Each it As ListViewItem In lvStudents.Items
  Dim t As Double
  If Double.TryParse(it.SubItems(3).Text, t) Then sum += t
Next
txtAvg.Text = (sum / lvStudents.Items.Count).ToString("F2")

' Assign grades: ensure subitem exists, then set text using a small helper that compares total to avg.

Cautions: validate at entry, keep avg calculation and grading separate, and use the debugger to catch index/out-of-range and parse exceptions quickly.

Recommended Answers

All 3 Replies

How are you storing the values for each student, that you want the averages of?

You have four button controls but you haven't told us what they do and you haven't even given them descriptive names. Instead, you dump the code here with almost no explanation and expect us to figure out what it is supposed to do. Put some effort into asking the question and we'll be happy to offer some suggestions.

and also try it to debug your codes.. to see the errors..

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.