1 Replies - 834 Views - Last Post: 21 July 2012 - 04:21 PM Rate Topic: -----

#1 dsgoodwin82  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 47
  • Joined: 08-October 11

ListBox values are not adding up

Posted 20 July 2012 - 07:15 PM

I have a list box where the user can pick multiple items from. The problem is, when the total button is clicked the items picked from ListBox1 should add together and their total be displayed in Label1. Label1 is only displaying the first value clicked. I am using visual studio 2008. Thanks.
Code is below:
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Total, Service, Grade, Quarts As Decimal

        Service = ListBox1.SelectedValue
        Grade = DropDownList1.SelectedValue
        Quarts = (TextBox1.Text * DropDownList1.SelectedValue)
        Total = (Service + Grade + Quarts)

        Response.Write(FormatCurrency(Service))
        Label1.Text = FormatCurrency(Service, 2)
        Response.Write(FormatCurrency(Grade))
        Label2.Text = FormatCurrency(Grade, 2)
        Response.Write(FormatCurrency(Quarts))
        Label3.Text = FormatCurrency(Quarts, 2)
        Response.Write(FormatCurrency(Total))
        Label4.Text = FormatCurrency(Total, 2)

        If Total = Service + Grade + Quarts Then
            Label5.Text = "Thank you!"
        End If

    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: ListBox values are not adding up

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: ListBox values are not adding up

Posted 21 July 2012 - 04:21 PM

SelectedValue only returns the value of the first selected item it comes to in the list. You will need to loop through the ListBox Items property and find each item that has been selected.

foreach (ListItem item in ListBox.Items)
{
  if (item.Selected)
  {
    // Do Something
  }
}



that's in c# but shouldn't be difficult to translate to VB

This post has been edited by Nakor: 21 July 2012 - 04:23 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1