Please I need help urgently in summing up several numbers in a list box using VB.NET. I,ve stayed too long tring to figure it out, but it all proved abortive. Looking forward to a reply soon.

Member Avatar for iamthwee

1) Drag a listbox onto your form
2) Drag a button onto your form

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim total As Integer = 0

        Dim i As Integer
        For i = 0 To ListBox1.Items.Count - 1 Step 1

            total = total + ListBox1.Items(i)

        Next
        MessageBox.Show(total.ToString)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Add(30)
        ListBox1.Items.Add(2)
        ListBox1.Items.Add(8)
    End Sub
End Class

I would probably check beforehand in the items in the box are valid integers/floating point numbers.

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.