473,395 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 5272
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

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

Similar topics

0
by: Jason Callas | last post by:
I have a weird problem with a sorted ListBox which is bound to an ArrayList. My ArrayList is holding a collection of custom objects. One of the public properties is called Name. When I add a few...
4
by: questions | last post by:
I create a Form and call another class (e.g. class1.cs). (class1.cs) contains a command to call back the Form to display a string variable in a listbox. However, it doesn't works. The listbox gets...
5
by: Alien2_51 | last post by:
I have a problem with a ListBox control that is on a TabControl, it seems to be forgetting which items are selected in the list when I tab off the current tab, here's my winform code... I even...
0
by: **Developer** | last post by:
Would someone pass this on to MS and/or check to see it the problem persists in 2005? I don't have 2005 and do not know how to pass it on. Nor do I have the time to spend learning how unless...
2
by: John | last post by:
Hi, I'm currently working on a simple project (for study on C#) with an input (maskedtextbox) and a listbox. The input numbers are send in the listbox. When the listbox is filled with numbers...
0
by: Andrew | last post by:
I have created a Component called a BOConnector that implements IBindingList so it can provide access to a list of business objects to be bound to controls. At design time there is no live list...
5
by: kimiraikkonen | last post by:
Hello, I have openfiledialog control named "openfileplaylist" and multi- selectpropert is TRUE. But although i select more than one files using "shift+arrows", i only get one file listed in my...
2
by: cshaw | last post by:
Hello Everyone, I am having problems with a listbox control. I have a page with a couple of labels and drop-down lists at the top, and then below there is a table with two columns, the first column...
2
by: lhsiber | last post by:
I am new to access and am having a problem with filtering. Here is a little bit of my setup: I have a main form that has a listbox so that users can choose one or many groups in which to display...
2
OuTCasT
by: OuTCasT | last post by:
I have a listbox on my form that has items that the user has chosen, i need to loop through the listbox and get the values and insert them into a sql table. Can anyone hlp?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.