user selects from multiple listboxes to get a price from a price array

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am new to vb and I am having so much trouble with this. What I need to do is this: I have a form application for winter sports equipment rental. I have a listbox filled with the equipment for the user to choose from then I have a combobox for the
user to select the duration they would like to rent it. The book that I am using has no examples I can refrence and i have had no luck online either. I am posting what I have so far. However, i think I am going in the wrong direction. I have been working
on this for so long that I think I have over thought it and made a mess of it. I am thinking that I need to add a new class for rentalRates then use enum of durationType and equipmentType but I am unsure how to move that way. That is using a enum, a 2d array
and a parallel array? I would greatly appreciate any suggestions.



<pre lang="x-vbnet Public Class WinterSportRentals

Private ReadOnly mNames() As String = {"Skis, beginner", "Skis, advanced", "Snowboard, beginner",
"Snowboard, advanced", "Ski boots", "Snowboard boots", "Helmet standard", "Helmet deluxe"}
Private ReadOnly mPrices(,) As Double = {{35, 45, 80, 150, 280},
{45, 55, 90, 170, 320},
{32, 42, 78, 148, 275},
{45, 55, 100, 175, 335},
{10, 15, 25, 50, 90},
{10, 15, 20, 45, 80},
{10, 12, 15, 50, 90},
{15, 20, 25, 70, 110}}

Private ReadOnly mDuration() As String = {"1 day", "2 days", "3 days", "1 week", "2 weeks"}
Private ReadOnly mDays() As Integer = {1, 2, 3, 7, 14}

Private mDeposit As Decimal
Private mTax As Double = 0.065
Private mFinalEquipCost As Decimal = 0.0
Private mTotalCostOfDaysRented As Decimal = 0.0
Private mSubtotal As Decimal = 0.0
Private mBalDue As Decimal
Private ReadOnly mDamageInsurance As Decimal = 12.0
Private ReadOnly mLiabilityWaiver As Decimal = 50.0

Private equipmentsToPrices(10) As EquipmentToPrices
Private durationsToPrices(5) As DurationToPrices

Public Overrides Function ToString() As String
Return "WinterSportRental"
End Function

Class EquipmentToPrices
Public Name As String
Public Index As Integer
Public Overrides Function ToString() As String
Return Name
End Function
End Class

Class DurationToPrices
Public Duration As String
Public Price As Double
Public Overrides Function ToString() As String
Return Duration
End Function
End Class

list the items in the list box
Private Sub WinterSportRentals_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

populate the objects
For i As Integer = 0 To mNames.Count - 1
equipmentsToPrices(i) = New EquipmentToPrices
equipmentsToPrices(i).Name = mNames(i)
equipmentsToPrices(i).Index = mPrices(i, 0)
For j As Integer = 0 To mDuration.Count - 1
durationsToPrices(j) = New DurationToPrices
durationsToPrices(j).Duration = mDuration(j)
durationsToPrices(j).Price = mPrices(0, j)
Next
cmBoxRentalDur.DataSource = durationsToPrices
Next
lstBoxEquipment.DataSource = equipmentsToPrices

change the time format
DateTimePicker1.Format = DateTimePickerFormat.Custom
DateTimePicker1.CustomFormat = "MMMM dd, yyyy"

End Sub

set the weather hyperlink
Private Sub lnkLblForecast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLblForecast.Click
lnkLblForecast.LinkVisited = True
System.Diagnostics.Process.Start("IExplore", "http://www.weather.com")
End Sub

close button
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub

process calculation
Private Sub btnCal_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCal.Click

Dim decSubtotal As Decimal = 0.0
Dim decBalDue As Decimal = 0.0
Dim decDeposit As Decimal = 0.0
Dim dblTotalTaxes As Decimal = 0.0
Dim dblTax As Double = 0.065
Dim decDamageInsurance As Decimal = 0.1D
Dim decLiabilityWaiver As Decimal = 50.0
Dim decTotalCost As Decimal = 0.0

Dim durIndex As Integer = cmBoxRentalDur.SelectedIndex

if duration index is valid
loop through the equipment list to see what is selected
look in the price array(equipIndex,durIndex)
If durIndex <> -1 Then
For Each equipIndex As Integer In lstBoxEquipment.SelectedIndices
Dim cost As Decimal = mPrices(equipIndex, durIndex)
add up the cost
decTotalCost = decTotalCost + cost
Next
Else
MessageBox.Show("You must select how rental duration")
End If

calculate taxes
dblTotalTaxes = decTotalCost * dblTax

if damage insurance requested, added to the subtotal
If chkBoxDamageIns.Checked Then
decSubtotal = decTotalCost + dblTotalTaxes + (decDamageInsurance * decTotalCost)
End If

if liability requested, added to the subtotal
If chkBoxLiability.Checked Then
decSubtotal = decSubtotal + decLiabilityWaiver
End If

Try
decDeposit = CDec(txtDeposit.Text)
decBalDue = decSubtotal - decDeposit

Catch
decDeposit = 0.0
txtDeposit.Text = "0.00"
decBalDue = decSubtotal - decDeposit
End Try

lblSubtotal.Text = decSubtotal.ToString("c")
lblTax.Text = dblTotalTaxes.ToString("n")
lblBalance.Text = decBalDue.ToString("c")

End Sub

when the duration is selected, it calcuate the total and store it
Private Sub cmBoxRentalDur_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmBoxRentalDur.SelectedIndexChanged


Dim equipmentType As Double = mNames(lstBoxEquipment.SelectedIndex)
Dim duration As Integer = mDuration(cmBoxRentalDur.SelectedItem)



End Sub

Private Sub lstBoxEquipment_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstBoxEquipment.SelectedIndexChanged

Dim selectedEquipment As EquipmentToPrices = CType(lstBoxEquipment.SelectedItem, EquipmentToPrices)

End Sub
End Class
[/code]
<br/>
<br/>


View the full article
 
Back
Top