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. - Public Class Form1
-
-
Private Sub btnAddWorkshop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 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.SelectedItem = "Handling Stress" Then
-
RegistrationFee = 595
-
Days = 3
-
ElseIf lstWorkshop.SelectedItem = "Time Management" Then
-
RegistrationFee = 695
-
Days = 3
-
ElseIf lstWorkshop.SelectedItem = "Supervision Skills" Then
-
RegistrationFee = 995
-
Days = 3
-
ElseIf lstWorkshop.SelectedItem = "Negotiation" Then
-
RegistrationFee = 1295
-
Days = 5
-
ElseIf lstWorkshop.SelectedItem = "How to Interview" Then
-
RegistrationFee = 395
-
Days = 1
-
End If
-
-
' Getting User selected Locations for Workshop
-
If lstLocation.SelectedItem = "Austin" Then
-
LodgingFee = 95
-
ElseIf lstLocation.SelectedItem = "Chicago" Then
-
LodgingFee = 125
-
ElseIf lstLocation.SelectedItem = "Dallas" Then
-
LodgingFee = 110
-
ElseIf lstLocation.SelectedItem = "Orlando" Then
-
LodgingFee = 100
-
ElseIf lstLocation.SelectedItem = "Phoenix" Then
-
LodgingFee = 92
-
ElseIf lstLocation.SelectedItem = "Raleigh" Then
-
LodgingFee = 90
-
End If
-
' Populate the Costs List Box
-
inttest2 = lstCosts.Items.Add(RegistrationFee + (LodgingFee * Days).ToString)
-
Return
-
End Sub
-
-
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
-
-
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.Items(0)) + (lstCosts.Items(X))
-
-
X += 1
-
Loop
-
-
' Display total
-
lblTotalCost.Text = intTest
-
End If
-
-
-
End Sub
-
-
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
-
' Reset the list Boxes by deselecting the currently selected items
-
lstWorkshop.SelectedIndex = -1
-
lstLocation.SelectedIndex = -1
-
lstCosts.Items.Clear()
-
lblTotalCost.Text = String.Empty
-
End Sub
-
-
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
-
' Exit program by closing window
-
Me.Close()
-
End Sub
-
End Class
8 5285
Does anybody have a solution for this code? Need Help!
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.
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!
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. - 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
Well Thanks for the solution! I appreciate your help!
Well Thanks for the solution! I appreciate your help!
So, that does what you want?
So, that does what you want?
Of course, it worked perfectly! Thank You!
Of course, it worked perfectly! Thank You!
Glad we could help. :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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?
|
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,...
|
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.
|
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.
| |
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...
|
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
|
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...
|
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...
|
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?
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |