Results 1 to 2 of 2

Thread: Calculating items in a list box

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    1

    Calculating items in a list box

    I'm going to apologize in advance for this; I googled this before and found plenty of answers, but for some reason none of them really clicked with me yet, and I'm still stumped.

    It's a school assignment that I can't quite get down. The goal is to create an application that can calculate the total cost of one or more workshops. It includes three list boxes; one to select a workshop, one to select a location, and one that displays the costs of the workshop and lodging fees (which vary by location). I am then supposed to add the numbers in the third listbox to come up with the total cost of all the workshops and lodging (you can select multiple), and display it in a texbox. I believe that I'm supposed to use a loop for this and have been trying them out all day, but can't get them to work.

    Here's the code I have so far.
    Code:
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Partial Class Form1
        Inherits System.Windows.Forms.Form
    
        'Form overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                If disposing AndAlso components IsNot Nothing Then
                    components.Dispose()
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub
    
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
            Me.lstWorkshop = New System.Windows.Forms.ListBox()
            Me.lstLocation = New System.Windows.Forms.ListBox()
            Me.GroupBox1 = New System.Windows.Forms.GroupBox()
            Me.GroupBox2 = New System.Windows.Forms.GroupBox()
            Me.GroupBox3 = New System.Windows.Forms.GroupBox()
            Me.lstCost = New System.Windows.Forms.ListBox()
            Me.Label1 = New System.Windows.Forms.Label()
            Me.lblCost = New System.Windows.Forms.Label()
            Me.btnAdd = New System.Windows.Forms.Button()
            Me.btnCalc = New System.Windows.Forms.Button()
            Me.Button3 = New System.Windows.Forms.Button()
            Me.Button4 = New System.Windows.Forms.Button()
            Me.GroupBox3.SuspendLayout()
            Me.SuspendLayout()
            '
            'lstWorkshop
            '
            Me.lstWorkshop.FormattingEnabled = True
            Me.lstWorkshop.Items.AddRange(New Object() {"Handling Stress", "Time Management", "Supervision Skills", "Negotiation", "How to Interview"})
            Me.lstWorkshop.Location = New System.Drawing.Point(68, 101)
            Me.lstWorkshop.Name = "lstWorkshop"
            Me.lstWorkshop.Size = New System.Drawing.Size(138, 121)
            Me.lstWorkshop.TabIndex = 0
            '
            'lstLocation
            '
            Me.lstLocation.FormattingEnabled = True
            Me.lstLocation.Items.AddRange(New Object() {"Austin", "Chicago", "Dallas", "Orlando", "Phoenix", "Raleigh"})
            Me.lstLocation.Location = New System.Drawing.Point(293, 101)
            Me.lstLocation.Name = "lstLocation"
            Me.lstLocation.Size = New System.Drawing.Size(149, 121)
            Me.lstLocation.TabIndex = 1
            '
            'GroupBox1
            '
            Me.GroupBox1.Location = New System.Drawing.Point(268, 80)
            Me.GroupBox1.Name = "GroupBox1"
            Me.GroupBox1.Size = New System.Drawing.Size(233, 161)
            Me.GroupBox1.TabIndex = 2
            Me.GroupBox1.TabStop = False
            Me.GroupBox1.Text = "Pick a Location"
            '
            'GroupBox2
            '
            Me.GroupBox2.Location = New System.Drawing.Point(47, 80)
            Me.GroupBox2.Name = "GroupBox2"
            Me.GroupBox2.Size = New System.Drawing.Size(200, 161)
            Me.GroupBox2.TabIndex = 3
            Me.GroupBox2.TabStop = False
            Me.GroupBox2.Text = "Pick a Workshop"
            '
            'GroupBox3
            '
            Me.GroupBox3.Controls.Add(Me.lstCost)
            Me.GroupBox3.Location = New System.Drawing.Point(550, 80)
            Me.GroupBox3.Name = "GroupBox3"
            Me.GroupBox3.Size = New System.Drawing.Size(200, 151)
            Me.GroupBox3.TabIndex = 4
            Me.GroupBox3.TabStop = False
            Me.GroupBox3.Text = "List of Costs"
            '
            'lstCost
            '
            Me.lstCost.FormattingEnabled = True
            Me.lstCost.Location = New System.Drawing.Point(17, 19)
            Me.lstCost.Name = "lstCost"
            Me.lstCost.Size = New System.Drawing.Size(161, 121)
            Me.lstCost.TabIndex = 0
            '
            'Label1
            '
            Me.Label1.AutoSize = True
            Me.Label1.Location = New System.Drawing.Point(290, 271)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(55, 13)
            Me.Label1.TabIndex = 1
            Me.Label1.Text = "Total Cost"
            '
            'lblCost
            '
            Me.lblCost.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
            Me.lblCost.Location = New System.Drawing.Point(383, 271)
            Me.lblCost.Name = "lblCost"
            Me.lblCost.Size = New System.Drawing.Size(80, 21)
            Me.lblCost.TabIndex = 5
            '
            'btnAdd
            '
            Me.btnAdd.Location = New System.Drawing.Point(131, 308)
            Me.btnAdd.Name = "btnAdd"
            Me.btnAdd.Size = New System.Drawing.Size(86, 33)
            Me.btnAdd.TabIndex = 6
            Me.btnAdd.Text = "Add Workshop"
            Me.btnAdd.UseVisualStyleBackColor = True
            '
            'btnCalc
            '
            Me.btnCalc.Location = New System.Drawing.Point(254, 318)
            Me.btnCalc.Name = "btnCalc"
            Me.btnCalc.Size = New System.Drawing.Size(94, 23)
            Me.btnCalc.TabIndex = 7
            Me.btnCalc.Text = "Calculate Total"
            Me.btnCalc.UseVisualStyleBackColor = True
            '
            'Button3
            '
            Me.Button3.Location = New System.Drawing.Point(383, 318)
            Me.Button3.Name = "Button3"
            Me.Button3.Size = New System.Drawing.Size(75, 23)
            Me.Button3.TabIndex = 8
            Me.Button3.Text = "Reset"
            Me.Button3.UseVisualStyleBackColor = True
            '
            'Button4
            '
            Me.Button4.Location = New System.Drawing.Point(508, 318)
            Me.Button4.Name = "Button4"
            Me.Button4.Size = New System.Drawing.Size(75, 23)
            Me.Button4.TabIndex = 9
            Me.Button4.Text = "Exit"
            Me.Button4.UseVisualStyleBackColor = True
            '
            'Form1
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(779, 363)
            Me.Controls.Add(Me.Button4)
            Me.Controls.Add(Me.Button3)
            Me.Controls.Add(Me.btnCalc)
            Me.Controls.Add(Me.btnAdd)
            Me.Controls.Add(Me.lblCost)
            Me.Controls.Add(Me.Label1)
            Me.Controls.Add(Me.GroupBox3)
            Me.Controls.Add(Me.lstLocation)
            Me.Controls.Add(Me.lstWorkshop)
            Me.Controls.Add(Me.GroupBox1)
            Me.Controls.Add(Me.GroupBox2)
            Me.Name = "Form1"
            Me.Text = "Form1"
            Me.GroupBox3.ResumeLayout(False)
            Me.ResumeLayout(False)
            Me.PerformLayout()
    
        End Sub
        Friend WithEvents lstWorkshop As System.Windows.Forms.ListBox
        Friend WithEvents lstLocation As System.Windows.Forms.ListBox
        Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
        Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
        Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents lblCost As System.Windows.Forms.Label
        Friend WithEvents btnAdd As System.Windows.Forms.Button
        Friend WithEvents btnCalc As System.Windows.Forms.Button
        Friend WithEvents Button3 As System.Windows.Forms.Button
        Friend WithEvents Button4 As System.Windows.Forms.Button
    
        Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    
    
            'Set variables
            Dim decStress As Decimal
            Dim decManagement As Decimal
            Dim decSupervision As Decimal
            Dim decNegotiation As Decimal
            Dim decInterview As Decimal
            Dim decAustin As Decimal
            Dim decChicago As Decimal
            Dim decDallas As Decimal
            Dim decOrlando As Decimal
            Dim decPhoenix As Decimal
            Dim decRaleigh As Decimal
            Dim strWorkshop As String
            Dim strLocation As String
            Dim strStress As String
            Dim strManagement As String
            Dim strSupervision As String
            Dim strNegotiation As String
            Dim strInterview As String
            Dim strAustin As String
            Dim strChicago As String
            Dim strDallas As String
            Dim strOrlando As String
            Dim strPhoenix As String
            Dim strRaleigh As String
            decStress = 595
            decManagement = 695
            decSupervision = 995
            decNegotiation = 1295
            decInterview = 395
    
            'Multiply lodging cost by days
            If lstWorkshop.SelectedIndex = 0 Or 1 Or 2 Then
                decAustin = 95 * 3
                decChicago = 125 * 3
                decDallas = 110 * 3
                decOrlando = 100 * 3
                decPhoenix = 92 * 3
                decRaleigh = 90 * 3
            ElseIf lstWorkshop.SelectedIndex = 3 Then
                decAustin = 95 * 5
                decChicago = 125 * 5
                decDallas = 110 * 5
                decOrlando = 100 * 5
                decPhoenix = 92 * 5
                decRaleigh = 90 * 5
            ElseIf lstWorkshop.SelectedIndex = 4 Then
                decAustin = 95
                decChicago = 125
                decDallas = 110
                decOrlando = 100
                decPhoenix = 92
                decRaleigh = 90
            End If
            strStress = "Handling Stress "
            strManagement = "Time Management "
            strSupervision = "Supervision Skills "
            strNegotiation = "Negotiation "
            strInterview = "How To Interview "
            strAustin = "Austin "
            strChicago = "Chicago "
            strDallas = "Dallas "
            strOrlando = "Orlando "
            strPhoenix = "Phoenix "
            strRaleigh = "Raleigh "
            lstWorkshop.Items(0) = decStress
            lstWorkshop.Items(1) = decManagement
            lstWorkshop.Items(2) = decSupervision
            lstWorkshop.Items(3) = decNegotiation
            lstWorkshop.Items(4) = decInterview
            lstLocation.Items(0) = decAustin
            lstLocation.Items(1) = decChicago
            lstLocation.Items(2) = decDallas
            lstLocation.Items(3) = decOrlando
            lstLocation.Items(4) = decPhoenix
            lstLocation.Items(5) = decRaleigh
            strWorkshop = lstWorkshop.SelectedItem.ToString()
            strLocation = lstLocation.SelectedItem.ToString()
    
    
            lstCost.Items.Insert(0, strWorkshop)
            lstCost.Items.Insert(1, strLocation)
            lstWorkshop.Items(0) = strStress
            lstWorkshop.Items(1) = strManagement
            lstWorkshop.Items(2) = strSupervision
            lstWorkshop.Items(3) = strNegotiation
            lstWorkshop.Items(4) = strInterview
            lstLocation.Items(0) = strAustin
            lstLocation.Items(1) = strChicago
            lstLocation.Items(2) = strDallas
            lstLocation.Items(3) = strOrlando
            lstLocation.Items(4) = strPhoenix
            lstLocation.Items(5) = strRaleigh
    
    
        End Sub
        Friend WithEvents lstCost As System.Windows.Forms.ListBox
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            lstWorkshop.SelectedIndex = -1
            lstLocation.SelectedIndex = -1
            lstCost.Items.Clear()
        End Sub
    
    End Class
    Scroll down to Private Sub btnAdd_Click to get to where I started

  2. #2
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Calculating items in a list box

    You have most of the statements that you will need but you're jumping into code before you've considered how this app will work. You need to think about the operation in advance of coding, in other words you need to have a 'mini-specification' in mind.

    On FormLoad, populate three ListBoxes with the various options (so need some code to load the ListBoxes)
    The user will select something in each ListBox (need the user to be able to select one and only one item per ListBox)
    When the user clicks the 'Add' button (need a button)
    - Check that three things are selected (check the number of Items selected in each ListBox)
    - If there are three (and only three), then take those selections and work out the cost (work out the cost, using the selections)

    It would be elegant and probably worth a higher grade if your options were held in Lists or Arrays, so you can populate the ListBoxes and work out the cost, by using the same source data.

    (Given this is a school assignment, I'll tell you what you need to do, but I'm not writing it for you).


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

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