Results 1 to 4 of 4

Thread: Adding values from Listboxes

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2014
    Posts
    7

    Post Adding values from Listboxes

    Today i was making a program for class with listboxes. The problem i ran into was that i assigned values to the contents inside the listboxes, but i don't know how to add the values together and display them into a label. I would like some help please, thanks.

    Here's the Problem from class: When a customer registers for a workshop, he or she must pay the registration fee plus the lodging fees for the selected location. For example, here are the charges to attend the Supervision Skills workshop in Orlando:
    Registration: $995
    Lodging: $100 x 3 days = $300
    Total: $1,295

    Here's the Code:
    Public Class Form1

    Private Sub btnAddWorkshop_Click(sender As System.Object, e As System.EventArgs) Handles btnAddWorkshop.Click
    ' Workshops
    Const dblHandlingSressFee As Double = 3 ' Handling Stress 3 Days
    Const dblTimeManagementFee As Double = 3 ' Time Management 3 Days
    Const dblSupervisionSkillsFee As Double = 3 ' Supervision Skills 3 Days
    Const dblNegotionFee As Double = 5 ' Negotion 5 Days
    Const dblHowToInterviewFee As Double = 1 ' How To Interview 1 Day

    ' Locations
    Const dblAustin As Double = 95.0 ' Austin
    Const dblChicago As Double = 125.0 ' Chicago
    Const dblDallas As Double = 110.0 ' Dallas
    Const dblOrlando As Double = 100.0 ' Orlando
    Const dblPhoenix As Double = 92.0 ' Phoenix
    Const dblRaleigh As Double = 90.0 ' Raleigh

    ' Listboxes
    Dim snglstListofcosts As String
    Dim snglstWorkshop As String
    Dim snglstLocation As String

    ' Display the selected Workshop and Location into the third Listbox
    lstListofcosts.Items.Add(lstWorkshop.Text)
    lstListofcosts.Items.Add(lstLocation.Text)

    If lstWorkshop.SelectedIndex = -1 And lstLocation.SelectedIndex = -1 Then
    MessageBox.Show("Please select a Workshop and Location")
    End If

    ' Assign values to the items in the listboxes
    lstWorkshop.SelectedIndex = 0 = 595D
    lstWorkshop.SelectedIndex = 1 = 695D
    lstWorkshop.SelectedIndex = 2 = 995D
    lstWorkshop.SelectedIndex = 3 = 1295D
    lstWorkshop.SelectedIndex = 4 = 395D

    lstLocation.SelectedIndex = 0 = 95D
    lstLocation.SelectedIndex = 1 = 125D
    lstLocation.SelectedIndex = 2 = 110D
    lstLocation.SelectedIndex = 3 = 100D
    lstLocation.SelectedIndex = 4 = 92D
    lstLocation.SelectedIndex = 5 = 90D
    End Sub

    Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
    lstListofcosts.Items.Clear()
    lstWorkshop.ClearSelected()
    lstLocation.ClearSelected()
    lblTotalCost.Text = String.Empty
    End Sub

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

    Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs) Handles btnCalculate.Click

    End Sub
    End Class

  2. #2
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Adding values from Listboxes

    To add a value to a listbox you use the listbox.Item.Add(<Value goes here>) method.

    Constants and Variables that need to be seen by procedures in your form need to be global to that form. i.e. Define them at the top of your form code above all the procedures.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  3. #3
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    756

    Re: Adding values from Listboxes

    if i understand correctly you want to go through the listbox and add all the values together?
    you would need a loop
    Code:
    Dim total as Decimal = 0
    For Each lineitem as String In Listbox1.Items
    If isnumeric(lineitem.ToString) then
    total = total + lineitem.ToString
    end if
    
    Next
    My CodeBank Submissions
    • Listbox with transparency and picture support - Click Here
    • Check for a true internet connection - Click Here
    • Open Cash drawer connected to receipt printer - Click Here
    • Custom color and size border around form - Click Here
    • Upload file to website without user logins, includes PHP - Click Here
    • List All Removable USB Storage Devices - Click Here
    • Custom On/Off Slide Control - Click Here
    • Insert multiple rows of data into one database table using parameters - Click Here
    • Trigger USB/Serial Cash Drawer - Click Here

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,414

    Re: Adding values from Listboxes

    You just want to add the currently selected value in listbox1 to the currently selected value in listbox2?

    Code:
    Dim value1, value2 as Decimal
    If Decimal.TryParse(ListBox1.Text, value1) AndAlso Decimal.TryParse(ListBox2.Text, value2) Then
        Label1.Text = (value1 + value2).ToString("c")
    End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width