i am doing one of the project from visual basic 2010 book.These are the requirements:
Workshop Selector:
3 list boxes, 4 buttons, 2 labels.
The application allows the user to select a workshop from lstWorkshop, Then select a location from lstLocation. Next btnAdd takes the selected workshop and location attached to a set amount of days and a registration fee and multiplies the days by a Lodging fee and outputs this number in dollar form. After multiple workshops/locations have been added btnCalc adds each of these and places them in lblTotal. The other two buttons are exit and clear which have already understand (of course the "easy" stuff)
i have finished most of the codings and it works ok, but now i need to code btnCalc (the final calculation) and i am planing on using the For.....Next Loop to do this calculation
my question is
if this is my code for the btnCalc button
how do i setup the intcount? over here saying saying 1 to 100 would be incurrect i think
How do i set it to infinity or should i set it to infinity ?
Code:
Dim intcount As Integer ' Loop counter
Dim intTotal As Integer = 0 ' Accumulator
'Add the numbers 1 through wat?
For intCount = 1 To 100
intTotal += intcount
Next
' Display the sum of the numbers.
lblcost.Text = intTotal.ToString("C")
Last edited by newtovb1111111111111; Jul 3rd, 2011 at 07:55 AM.
if u set a loop to infinity ur program will go into a endless loop and prob freeze ur pc..ok theres a million and 1 ways to do any 1 thing in programmin. one of the ways i might do this is declare an arraylist outside both the btnAdd & btnCalc event handlers. so this arrayList sits by itself in ur class. Then btnAdd would add the final price of ech location to the arrayList. An array or arrayList is just a variable which holds more than one value, and the values are given an index number so u can retrieve each value. then ur btnCalc will loop thro the arrayList and add then display the sum.. Like so:
vb Code:
public Class myVB2010BookClass
Private wrkshopTotals as New ArrayList()
sub btnAdd_Click(ByVal Sender as Object, ByVal e as System.EventArgs) Handles btnAdd.Click
'********Clear any previous values stored in arraylist
wrkshopTotals.Clear()
'********Your code here
'********Add the total for each workshop to the array as an individual item
wrkshopTotals.Add(totalForAworkshop)
End Sub
Sub btnCalc_Click(ByVal Sender as Object, ByVal e as System.EventArgs) Handles btnCalc.Click
Dim GrandTotal as Integer = 0
'********Loop through array and sum the price of all workshops
'********You could also use For Each instead
For i as integer = 0 to (wrkshopTotals.Count - 1)
GrandTotal = GrandTotal + Cint(wrkshopTotals(i))
Next
' Display the sum of the numbers as a dollar amount.
lblcost.Text = "$" & GrandTotal
End Sub
End Class
Hope it helps point u in da right direction. Cheers
Edit: OK.. Just noticed ur zipfile.. I'll DL it and take a look... prob wont get back till tonight.. I gotta clean my nightstand (bunch of bills and misc paper) B4 my girl kills me, then i gotta study for my math test... But i think my post should help nonetheless....
Last edited by elielCT; Jul 3rd, 2011 at 09:49 AM.
Reason: Just notice Zip file.. unghhhh!
Well it seems i may not be able to look at it after all! Im running a recovery tool on one of my drives, and its pretty big.. The recovery agent is using so much resources i cant seem to open visual studio.. it just keeps loading.. The recovery might not be done for a week or two... Take a look at my last post tho...
ok this is the code, it's missing the code for Clear button cause i havent put any codes in there but thats easy and i know how to code that part. the main problem is calculating the final cost which should be in the btnCalculateTotal_Click, i am trying to figur out how to make that code.
Code:
Public Class Form1
Private Sub btnAddWorkshop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddWorkshop.Click
' Variables
Dim strTotal As String
Dim intCalculate As Integer
' Handling Stress in different cities.
If lstPickWorkshop.SelectedIndex = 0 And lstPickLocation.SelectedIndex = 0 Then
intCalculate = CInt(595 + (95 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 0 And lstPickLocation.SelectedIndex = 1 Then
intCalculate = CInt(595 + (125 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 0 And lstPickLocation.SelectedIndex = 2 Then
intCalculate = CInt(595 + (110 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 0 And lstPickLocation.SelectedIndex = 3 Then
intCalculate = CInt(595 + (100 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 0 And lstPickLocation.SelectedIndex = 4 Then
intCalculate = CInt(595 + (92 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 0 And lstPickLocation.SelectedIndex = 5 Then
intCalculate = CInt(595 + (90 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
' Time Management cost in different cities.
ElseIf lstPickWorkshop.SelectedIndex = 1 And lstPickLocation.SelectedIndex = 0 Then
intCalculate = CInt(695 + (95 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 1 And lstPickLocation.SelectedIndex = 1 Then
intCalculate = CInt(695 + (125 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 1 And lstPickLocation.SelectedIndex = 2 Then
intCalculate = CInt(695 + (110 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 1 And lstPickLocation.SelectedIndex = 3 Then
intCalculate = CInt(695 + (100 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 1 And lstPickLocation.SelectedIndex = 4 Then
intCalculate = CInt(695 + (92 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 1 And lstPickLocation.SelectedIndex = 5 Then
intCalculate = CInt(695 + (90 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
' Supervision skills in different cities.
ElseIf lstPickWorkshop.SelectedIndex = 2 And lstPickLocation.SelectedIndex = 0 Then
intCalculate = CInt(995 + (95 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 2 And lstPickLocation.SelectedIndex = 1 Then
intCalculate = CInt(995 + (125 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 2 And lstPickLocation.SelectedIndex = 2 Then
intCalculate = CInt(995 + (110 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 2 And lstPickLocation.SelectedIndex = 3 Then
intCalculate = CInt(995 + (100 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 2 And lstPickLocation.SelectedIndex = 4 Then
intCalculate = CInt(995 + (92 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 2 And lstPickLocation.SelectedIndex = 5 Then
intCalculate = CInt(995 + (90 * 3))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
' Negotiation in different cities.
ElseIf lstPickWorkshop.SelectedIndex = 3 And lstPickLocation.SelectedIndex = 0 Then
intCalculate = CInt(1295 + (95 * 5))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 3 And lstPickLocation.SelectedIndex = 1 Then
intCalculate = CInt(1295 + (125 * 5))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 3 And lstPickLocation.SelectedIndex = 2 Then
intCalculate = CInt(1295 + (110 * 5))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 3 And lstPickLocation.SelectedIndex = 3 Then
intCalculate = CInt(1295 + (100 * 5))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 3 And lstPickLocation.SelectedIndex = 4 Then
intCalculate = CInt(1295 + (92 * 5))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 3 And lstPickLocation.SelectedIndex = 5 Then
intCalculate = CInt(1295 + (90 * 5))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
' How to interview in different cities.
ElseIf lstPickWorkshop.SelectedIndex = 4 And lstPickLocation.SelectedIndex = 0 Then
intCalculate = CInt(395 + (95 * 1))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 4 And lstPickLocation.SelectedIndex = 1 Then
intCalculate = CInt(395 + (125 * 1))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 4 And lstPickLocation.SelectedIndex = 2 Then
intCalculate = CInt(395 + (110 * 1))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 4 And lstPickLocation.SelectedIndex = 3 Then
intCalculate = CInt(395 + (100 * 1))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 4 And lstPickLocation.SelectedIndex = 4 Then
intCalculate = CInt(395 + (92 * 1))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
ElseIf lstPickWorkshop.SelectedIndex = 4 And lstPickLocation.SelectedIndex = 5 Then
intCalculate = CInt(395 + (90 * 1))
strTotal = intCalculate.ToString("c")
lstOutput.Items.Add(strTotal)
'Data Validation
ElseIf lstPickWorkshop.SelectedIndex = -1 Then
MessageBox.Show("You Must select a Workshop.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf lstPickLocation.SelectedIndex = -1 Then
MessageBox.Show("You Must select a Location.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
' Close th form.
If MessageBox.Show("Are You sure?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Me.Close()
End If
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
End Sub
Private Sub btnCalculateTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateTotal.Click
End Sub
End Class
Last edited by newtovb1111111111111; Jul 3rd, 2011 at 10:27 AM.
i is a variable... stands for integer... i is basicly being used to hold the value for number of items in lstOutPut starting from 0, and counting until it reaches the amount of list items in lstOutPut minus 1.. minus 1 because the first item in a listbox is item#0 not 1! the same with arrays, and just about everything else in programming.. number line starts from #0 not 1!
im glad i could be of help, hope u can see other ways to write code instead of huge if else statements...
dont forget to mark this post as resolved when resolved..
Original post was in 2010, but this may help future viewers Public Class frmWS
Dim dblFee, dblStay, dblPerDiem As Double
Dim decSum As Decimal ' store the total
Private Sub btnWSAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWSAdd.Click
Try
Dim intRNamecounter As Integer 'Result Name Counter
Dim intWScounter As Integer 'Workshop Name Counter
Dim intRLoccounter As Integer 'Result Location Counter
Dim intLoccounter As Integer
Dim intDup As Integer = 0 'Duplicate Workshop Name identified for input = 1
For intRNamecounter = 0 To lstRName.Items.Count - 1 'Cycle through Result Names "RName"
For intWScounter = 0 To lstWS.Items.Count - 1 'Cycle through WorkShop Names "WS"
For intRLoccounter = 0 To lstRLoc.Items.Count - 1 'Cycle trough Result Location "RLoc"
For intLoccounter = 0 To lstLoc.Items.Count - 1 'Cycle through Location "Loc"
If lstRName.Items.Item(intRNamecounter) = lstWS.SelectedItem And lstRLoc.Items.Item(intRLoccounter) = lstLoc.SelectedItem Then 'Find Duplicate WorkShop lstWS.Items.Item(intWScounter)
'MessageBox.Show("Selected Value" & lstRName.Items.Item(intRNamecounter).ToString & " " & lstWS.Items.Item(intWScounter).ToString)
'MessageBox.Show(" at " & lstLoc.SelectedItem)
intDup = 1
Else
End If
Next
Next
Next
Next
If intDup = 0 Then
If lstWS.SelectedIndex = -1 Then 'None Selected
MessageBox.Show("No WORKSHOP Item Has Been Selected")
ElseIf lstLoc.SelectedIndex = -1 Then 'None Selected
MessageBox.Show("No LOCATION Item Has Been Selected")
Else
Select Case lstLoc.SelectedIndex 'Cost Per Day
Case Is = 0
dblPerDiem = 95
Case Is = 1
dblPerDiem = 125
Case Is = 2
dblPerDiem = 110
Case Is = 3
dblPerDiem = 100
Case Is = 4
dblPerDiem = 92
Case Is = 5
dblPerDiem = 90
End Select
Select Case lstWS.SelectedIndex 'Registration Fee and Duration
Case Is = 0
dblFee = 595
dblStay = 3
Case Is = 1
dblFee = 695
dblStay = 3
Case Is = 2
dblFee = 995
dblStay = 3
Case Is = 3
dblFee = 1295
dblStay = 5
Case Is = 4
dblFee = 395
dblStay = 1
End Select
lstRName.Items.Add(lstWS.SelectedItem) 'Add Item to list box
lstRLoc.Items.Add(lstLoc.SelectedItem) 'Add Item to list box
lstResult.Items.Add(FormatCurrency(dblFee + (dblPerDiem * dblStay))) 'Add Item to list box
lstWS.SelectedItems.Clear()
lstLoc.SelectedItems.Clear()
lstWS.SelectedItems.Clear() 'Clear list box for counting duplicates
lstRName.ClearSelected()
lstLoc.ClearSelected()
lstRLoc.ClearSelected()
End If
Catch
End Try
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
lstWS.SelectedItems.Clear() ' clear all listboxes and Total label
lstLoc.SelectedItems.Clear()
lstResult.Items.Clear()
lstRName.Items.Clear()
lblResult.Text = ""
lblSum.Text = ""
decSum = 0
End Sub
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
decSum = 0 'Clears Total to prevent run-on total
Dim intcounter As Integer 'counter
For intcounter = 0 To lstResult.Items.Count - 1
decSum += CInt(lstResult.Items(intcounter))
Next
lblSum.Text = decSum.ToString("c") 'Display the total cost
End Sub
Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
Me.Close()
End Sub
Private Sub lstResult_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstResult.DoubleClick
lstResult.Items.Remove(lstResult.SelectedItem.ToString)