Problem with Loop and ListBox (VB 2005)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kappadon5
    New Member
    • Oct 2006
    • 1

    Problem with Loop and ListBox (VB 2005)

    Hello All, I am new to programming and I apologize in advance if I am out of protocol in any way shape or fashion. My problem is that I have a program where you select an option from two different list boxes which will in turn populate a third list box with a numeric number when you click a button. Then I have a calculate button that is supposed to add all the numbers that were populated in the third list box. I can only get the first item and the last item from the third list box to be added correctly. I cannot get all the numbers in between added. I am hoping someone might help me with my problem. I think my issue is how I have my loop set up but I cannot figure it out. Thanks in advance, I have taken the liberty of adding my source code at the bottom.

    [CODE=vbnet]Public Class Form1

    Private Sub btnAddWorkshop_ Click(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnAddWorkshop. Click

    ' Declaring Items for Calculations

    Dim RegistrationFee As Integer
    Dim LodgingFee As Integer
    Dim Days As Integer
    Dim inttest2 As Integer

    ' Getting User selected Workshop and Number of Days for workshop
    If lstWorkshop.Sel ectedItem = "Handling Stress" Then
    RegistrationFee = 595
    Days = 3
    ElseIf lstWorkshop.Sel ectedItem = "Time Management" Then
    RegistrationFee = 695
    Days = 3
    ElseIf lstWorkshop.Sel ectedItem = "Supervisio n Skills" Then
    RegistrationFee = 995
    Days = 3
    ElseIf lstWorkshop.Sel ectedItem = "Negotiatio n" Then
    RegistrationFee = 1295
    Days = 5
    ElseIf lstWorkshop.Sel ectedItem = "How to Interview" Then
    RegistrationFee = 395
    Days = 1
    End If

    ' Getting User selected Locations for Workshop
    If lstLocation.Sel ectedItem = "Austin" Then
    LodgingFee = 95
    ElseIf lstLocation.Sel ectedItem = "Chicago" Then
    LodgingFee = 125
    ElseIf lstLocation.Sel ectedItem = "Dallas" Then
    LodgingFee = 110
    ElseIf lstLocation.Sel ectedItem = "Orlando" Then
    LodgingFee = 100
    ElseIf lstLocation.Sel ectedItem = "Phoenix" Then
    LodgingFee = 92
    ElseIf lstLocation.Sel ectedItem = "Raleigh" Then
    LodgingFee = 90
    End If
    ' Populate the Costs List Box
    inttest2 = lstCosts.Items. Add(Registratio nFee + (LodgingFee * Days).ToString)
    Return
    End Sub

    Private Sub btnCalculate_Cl ick(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnCalculate.Cl ick

    Dim intTest As Integer
    Dim X As Integer = 1

    ' Display Error message if no workshop and selection have not been selected
    If lstCosts.Items. Count = 0 Then
    MessageBox.Show ("Please select a Workshop and Location!", "Roland Toussaint--Error")
    Else
    ' I think my problem is here somewhere ******
    ' Add items from list for display
    Do While X <> lstCosts.Items. Count

    intTest = CInt(lstCosts.I tems(0)) + (lstCosts.Items (X))

    X += 1
    Loop

    ' Display total
    lblTotalCost.Te xt = intTest
    End If


    End Sub

    Private Sub btnReset_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnReset.Click
    ' Reset the list Boxes by deselecting the currently selected items
    lstWorkshop.Sel ectedIndex = -1
    lstLocation.Sel ectedIndex = -1
    lstCosts.Items. Clear()
    lblTotalCost.Te xt = String.Empty
    End Sub

    Private Sub btnExit_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btnExit.Click
    ' Exit program by closing window
    Me.Close()
    End Sub
    End Class[/CODE]
    Last edited by Killer42; Nov 29 '07, 01:48 AM. Reason: Added CODE=vbnet tag
  • pumasr10
    New Member
    • Nov 2007
    • 4

    #2
    Does anybody have a solution for this code? Need Help!

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Sorry to see it has taken so long to get a response. I'm afraid we're a little short on VB experts these days.

      I think the problem is in line 62 (since I added a CODE=vbnet tag around your source, the lines are now numbered).
      This line says to add together the first and "Xth" items in the list, and place the result in intTest. So, each time around the loop you are generating a new value in intTest, based on the first item and the item you're up to.

      What you should be doing is simply adding each item to what's currently in intTest.

      By the way, I'd recommend using a For loop rather than a Do loop. There's no practical difference, but the For loop will make it much more obvious what the code does, to anyone reading the program in future.

      As for "taking the liberty" of including your source code, I wish more people would do so. Far too often people give a really detailed description like "I'm trying to write an accounting system and it doesn't work" and expect us to debug it somehow. Posting the code allows us to see exactly what is really going on.

      Oh, and I've deleted the duplicate thread you started today, pumasr10.
      Last edited by Killer42; Nov 29 '07, 01:59 AM.

      Comment

      • pumasr10
        New Member
        • Nov 2007
        • 4

        #4
        So all I have to do is change the value of the (0) and the (X) in line 62? Please correct me if I'm wrong. Thank you in advance!

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by pumasr10
          So all I have to do is change the value of the (0) and the (X) in line 62? Please correct me if I'm wrong. Thank you in advance!
          Um... try this.

          [CODE=vbnet]For X = 0 To (lstCosts.Items .Count - 1)
          intTest = intTest + lstCosts.Items( X)
          ' Or perhaps this is the way to write it...
          ' intTest += lstCosts.Items( X)
          Next[/CODE]
          Last edited by Killer42; Nov 29 '07, 09:58 PM.

          Comment

          • pumasr10
            New Member
            • Nov 2007
            • 4

            #6
            Well Thanks for the solution! I appreciate your help!

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by pumasr10
              Well Thanks for the solution! I appreciate your help!
              So, that does what you want?

              Comment

              • pumasr10
                New Member
                • Nov 2007
                • 4

                #8
                Originally posted by Killer42
                So, that does what you want?
                Of course, it worked perfectly! Thank You!

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by pumasr10
                  Of course, it worked perfectly! Thank You!
                  Glad we could help. :)

                  Comment

                  Working...