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 5272
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |