469,912 Members | 1,871 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,912 developers. It's quick & easy.

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.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub btnAddWorkshop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddWorkshop.Click
  4.  
  5.         ' Declaring Items for Calculations
  6.  
  7.         Dim RegistrationFee As Integer
  8.         Dim LodgingFee As Integer
  9.         Dim Days As Integer
  10.         Dim inttest2 As Integer
  11.  
  12.         ' Getting User selected Workshop and Number of Days for workshop
  13.         If lstWorkshop.SelectedItem = "Handling Stress" Then
  14.             RegistrationFee = 595
  15.             Days = 3
  16.         ElseIf lstWorkshop.SelectedItem = "Time Management" Then
  17.             RegistrationFee = 695
  18.             Days = 3
  19.         ElseIf lstWorkshop.SelectedItem = "Supervision Skills" Then
  20.             RegistrationFee = 995
  21.             Days = 3
  22.         ElseIf lstWorkshop.SelectedItem = "Negotiation" Then
  23.             RegistrationFee = 1295
  24.             Days = 5
  25.         ElseIf lstWorkshop.SelectedItem = "How to Interview" Then
  26.             RegistrationFee = 395
  27.             Days = 1
  28.         End If
  29.  
  30.         ' Getting User selected Locations for Workshop
  31.         If lstLocation.SelectedItem = "Austin" Then
  32.             LodgingFee = 95
  33.         ElseIf lstLocation.SelectedItem = "Chicago" Then
  34.             LodgingFee = 125
  35.         ElseIf lstLocation.SelectedItem = "Dallas" Then
  36.             LodgingFee = 110
  37.         ElseIf lstLocation.SelectedItem = "Orlando" Then
  38.             LodgingFee = 100
  39.         ElseIf lstLocation.SelectedItem = "Phoenix" Then
  40.             LodgingFee = 92
  41.         ElseIf lstLocation.SelectedItem = "Raleigh" Then
  42.             LodgingFee = 90
  43.         End If
  44.         ' Populate the Costs List Box
  45.         inttest2 = lstCosts.Items.Add(RegistrationFee + (LodgingFee * Days).ToString)
  46.         Return
  47.     End Sub
  48.  
  49.     Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
  50.  
  51.         Dim intTest As Integer
  52.         Dim X As Integer = 1
  53.  
  54.         ' Display Error message if no workshop and selection have not been selected
  55.         If lstCosts.Items.Count = 0 Then
  56.             MessageBox.Show("Please select a Workshop and Location!", "Roland Toussaint--Error")
  57.         Else
  58.              '  I think my problem is here somewhere ******
  59.             ' Add items from list for display
  60.             Do While X <> lstCosts.Items.Count
  61.  
  62.                 intTest = CInt(lstCosts.Items(0)) + (lstCosts.Items(X))
  63.  
  64.                 X += 1
  65.             Loop
  66.  
  67.             ' Display total
  68.             lblTotalCost.Text = intTest
  69.         End If
  70.  
  71.  
  72.     End Sub
  73.  
  74.     Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
  75.         ' Reset the list Boxes by deselecting the currently selected items
  76.         lstWorkshop.SelectedIndex = -1
  77.         lstLocation.SelectedIndex = -1
  78.         lstCosts.Items.Clear()
  79.         lblTotalCost.Text = String.Empty
  80.     End Sub
  81.  
  82.     Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
  83.         ' Exit program by closing window
  84.         Me.Close()
  85.     End Sub
  86. End Class
Oct 8 '06 #1
8 5028
Does anybody have a solution for this code? Need Help!
Nov 28 '07 #2
Killer42
8,435 Expert 8TB
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.
Nov 29 '07 #3
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!
Nov 29 '07 #4
Killer42
8,435 Expert 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. For X = 0 To (lstCosts.Items.Count - 1)
  2.   intTest = intTest + lstCosts.Items(X)
  3.   ' Or perhaps this is the way to write it...
  4.   ' intTest += lstCosts.Items(X)
  5. Next
Nov 29 '07 #5
Well Thanks for the solution! I appreciate your help!
Nov 29 '07 #6
Killer42
8,435 Expert 8TB
Well Thanks for the solution! I appreciate your help!
So, that does what you want?
Nov 30 '07 #7
So, that does what you want?
Of course, it worked perfectly! Thank You!
Nov 30 '07 #8
Killer42
8,435 Expert 8TB
Of course, it worked perfectly! Thank You!
Glad we could help. :)
Dec 9 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Jason Callas | last post: by
4 posts views Thread by questions | last post: by
2 posts views Thread by John | last post: by
reply views Thread by Andrew | last post: by
5 posts views Thread by kimiraikkonen | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.