0

This is what I have, just not sure how to actually get the sum of the listbox numbers.

    If intNumberOfEntries > 1 Then
        lblTotalSnowfall.Visible = True
        decTotalSnowfall = lstSnowfall.Items.Add(decSnowfall)
        lblTotalSnowfall.Text = "The total snowfall for the city is " &
            decTotalSnowfall.ToString("F1") & " inches"
    Else
        MsgBox("No snowfall value entered")
    End If

I know for sure line 3 is where I went wrong, just not sure what to put there. Thanks ahead for the help!

2
Contributors
2
Replies
25
Views
2 Weeks
Discussion Span
Last Post by ez4u2c
0

If it helps any at all, this is the full coding:

Public Class frmLakeEffect
Private Sub btnSnowfall_Click(sender As Object, e As EventArgs) Handles btnSnowfall.Click
    ' The btnSnowfall_Click event accepts and displays up to 7 snowfall values
    ' and then calculates and displays the total snowfall for the city in the months entered.

    ' Declare and initialize variables

    Dim strSnowfall As String
    Dim decSnowfall As Decimal
    Dim decTotalSnowfall As Decimal = 0D
    Dim strInputMessage As String = "Enter the snowfall for month #"
    Dim strInputHeading As String = "Snowfall"
    Dim strNormalMessage As String = "Enter the snowfall for month #"
    Dim strNonNumericError As String = "Error - Enter a number for the snowfall of month #"
    Dim strNegativeError As String = "Error - Enter a positive number for the snowfall of month #"

    ' Declare and initialize loop variables

    Dim strCancelClicked As String = ""
    Dim intMaxNumberOfEntries As Integer = 7
    Dim intNumberOfEntries As Integer = 1

    ' This loop allows the user to enter the snowfall of up to 7 months.
    ' The loop terminates when the user has entered 7 snowfall values or 
    ' the user taps or clicks the Cancel button or the Close button in the InputBox.

    strSnowfall = InputBox(strInputMessage & intNumberOfEntries, strInputHeading, " ")

    Do Until intNumberOfEntries > intMaxNumberOfEntries Or strSnowfall = strCancelClicked

        If IsNumeric(strSnowfall) Then
            decSnowfall = Convert.ToDecimal(strSnowfall)
            If decSnowfall > 0 Then
                lstSnowfall.Items.Add(decSnowfall)
                decTotalSnowfall += decSnowfall
                intNumberOfEntries += 1
                strInputMessage = strNormalMessage
            Else
                strInputMessage = strNegativeError
            End If
        Else
            strInputMessage = strNonNumericError
        End If

        If intNumberOfEntries <= intMaxNumberOfEntries Then
            strSnowfall = InputBox(strInputMessage & intNumberOfEntries, strInputHeading, " ")
        End If

    Loop

    ' Calculates and displays total snowfall
    If intNumberOfEntries > 1 Then
        lblTotalSnowfall.Visible = True
        decTotalSnowfall = lstSnowfall.Items.Add(decSnowfall)
        lblTotalSnowfall.Text = "The total snowfall for the city is " &
            decTotalSnowfall.ToString("F1") & " inches"
    Else
        MsgBox("No snowfall value entered")
    End If

    ' Disables the Snowfall button
    btnSnowfall.Enabled = False

    End Sub
0

Here is a snippet that sums all the items in column 1 (2nd column) of a listbox named "lstSnowfall" by using for..next to iterate through the list, starting with row 1 (assuming row 0 is a header).

Private Sub btnSnowfall_Click()
Dim i As Integer, Inches As Single
    For i = 1 To Me.lstSnowfall.ListCount - 1 'just summing all data in listbox
        Inches = Inches + Val(Me.lstSnowfall.Column(1, i)) 'assuming snowfall data in column 1 (2nd column)
    Next i
    MsgBox "Snowfall = " & Inches & " inches"
End Sub

Edited by ez4u2c

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.