473,769 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Loop and ListBox (VB 2005)

1 New Member
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 5285
pumasr10
4 New Member
Does anybody have a solution for this code? Need Help!
Nov 28 '07 #2
Killer42
8,435 Recognized Expert Expert
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
pumasr10
4 New Member
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 Recognized Expert Expert
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
pumasr10
4 New Member
Well Thanks for the solution! I appreciate your help!
Nov 29 '07 #6
Killer42
8,435 Recognized Expert Expert
Well Thanks for the solution! I appreciate your help!
So, that does what you want?
Nov 30 '07 #7
pumasr10
4 New Member
So, that does what you want?
Of course, it worked perfectly! Thank You!
Nov 30 '07 #8
Killer42
8,435 Recognized Expert Expert
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
1317
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 objects to the list and then bind the ArrayList to the ListBox (and after setting the DisplayMemeber property to "Name") the names show up like that are supposed to. If I then add a new element to the ArrayList and call the Refresh method of the...
4
1764
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 no response. Command: listBox.Items.Add( str ); However, it works when i type "MessageBox.Show( str);". A correct string displays in the popup message box. Can anyone help me?
5
2197
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 added a click event handler that resets the selected items based on whats in the collection it is data bound to... I'm baffled, pounding my head against the wall, it's not working... Please help... I tried posting the code but it was too long,...
0
1102
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 it really easy. However, I spent much time on this and would like to see what I've learned put to use.
2
8921
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 it automaticly puts the scrollbar in the listbox, thats fine but it doesn't scroll down to the last number that was keyed in and isn't visible. You have to scroll down in the listbox itself to make the last number that was entered visible.
0
1204
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 of objects so BOConnector creates an internal list of one object. It needs one object because when you bind to a grid etc at design time it needs to get a list of properties to make the columns. My BOConnector works just fine with grids and...
5
2291
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 listbox. What's wrong? Code: If openfileplaylist.ShowDialog() = Windows.Forms.DialogResult.OK Then
2
3933
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 contains a listbox and the second column contains some buttons. I am trying to display it such that if the listbox is empty it will be at least 100px wide, but if there is content in the box I want it to expand dynamically such that if the...
2
2056
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 information about. The control name of the listbox is "grupos". The name of the field it is sorting is "Grupo_Nombre". From the user´s selection, they can open up differing forms using command buttons that display filtered information about either...
2
1932
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
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.