Question Concatenation to list box problem

william7

Member
Joined
Mar 2, 2013
Messages
10
Programming Experience
Beginner
I've tried everything, but can't get the list box in my second form to put out the number of passengers selected and the total cost from the code below. All I get is 0s. Can anybody give me a hand?


VB.NET:
Option Strict On


Public Class MainForm

    Dim Destinations() As String = {"Boston", "Miami", "Dallas", "Denver", "Phoenix", "Seattle"}
    Dim Airports() As String = {"Logan", "Coral", "Alamo", "Rocky", "Queen", "Olympia"}
    Dim Prices() As Double = {"499.99", "585.25", "475.99", "605.33", "799.99", "876.34"}
    Dim intSIZE As Integer = Destinations.Length - 1
    Dim TotalCost(intSIZE) As Double
    Dim TotalPassengers(intSIZE) As Integer


    Private Sub MainForm_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick

        Dim Y As Integer


        Summary.lstBookings.Items.Clear()

        For Y = 0 To intSIZE

            Summary.lstBookings.Items.Add(Destinations(Y).ToString & "has " & TotalPassengers(Y).ToString & "booked for a total cost of " & FormatCurrency(TotalCost(Y).ToString))

        Next


        Summary.ShowDialog()

    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim strCities As String


        Timer1.Enabled = True
        Timer1.Interval = 1000

        btnBook.Enabled = False
        cboPassengers.Enabled = False

        For Each strCities In Destinations
            cboCities.Items.Add(strCities)

        Next
    End Sub

    Sub GetTimes()

        Dim now As DateTime = DateTime.Now
        Dim Boston = System.TimeZoneInfo.ConvertTime(now, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"))
        Dim Miami = System.TimeZoneInfo.ConvertTime(now, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"))
        Dim Dallas = System.TimeZoneInfo.ConvertTime(now, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"))
        Dim Denver = System.TimeZoneInfo.ConvertTime(now, TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time"))
        Dim Phoenix = System.TimeZoneInfo.ConvertTime(now, TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time"))
        Dim Seattle = System.TimeZoneInfo.ConvertTime(now, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"))

        txtBoston.Text = Boston.ToLongTimeString
        txtMiami.Text = Miami.ToLongTimeString
        txtDallas.Text = Dallas.ToLongTimeString
        txtDenver.Text = Denver.ToLongTimeString
        txtPhoenix.Text = Phoenix.ToLongTimeString
        txtSeattle.Text = Seattle.ToLongTimeString

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        GetTimes()
    End Sub

    Private Sub cboCities_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCities.SelectedIndexChanged

        Dim X As Integer



        X = cboCities.SelectedIndex



        txtAirport.Text = Airports(5).ToString
        txtPrice.Text = Prices(5).ToString("C")

        If X >= 0 Then

            cboPassengers.Enabled = True

        End If



    End Sub

    Private Sub cboPassengers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPassengers.SelectedIndexChanged

        Dim decCost As Decimal

        If cboPassengers.SelectedIndex >= 0 Then

            decCost = CInt(cboPassengers.SelectedItem) * CDec(txtPrice.Text)
            txtCost.Text = decCost.ToString("C2")
            btnBook.Enabled = True

        End If
       

    End Sub

    Private Sub btnBook_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBook.Click

        Dim X As String

        If cboCities.SelectedIndex >= 0 AndAlso cboPassengers.SelectedIndex >= 0 Then

            X = CStr(cboCities.SelectedItem)
            TotalPassengers(intSIZE) += CInt(cboPassengers.SelectedItem)
            TotalCost(intSIZE) += CDbl(txtCost.Text)
            MsgBox("Trip to " & X.ToString & " for " & txtCost.Text & " booked for " & TotalPassengers(intSIZE).ToString & " passengers")
            txtCost.Text = Nothing
            txtPrice.Text = Nothing
            cboCities.SelectedIndex = -1
            cboPassengers.SelectedIndex = -1

        End If

       
    End Sub
End Class

8pAefpU.jpg
 
Hi,

You have got a few issues going on here:-

1) You do not load any entries into the Passenger ComboBox therefore the cboPassengers_SelectedIndexChanged event can NEVER fire to enable the Booking Button which then saves your booking details.

2) Once you have sorted point 1 and you then make a Booking you always update the same element of your TotalPassengers and TotalCost arrays due to your use of the intSIZE variable.

3) In addition to point 2, your message after a booking is made, is displaying the wrong variables. i.e:-

VB.NET:
MsgBox("Trip to " & X.ToString & " for " & txtCost.Text & " booked for " & TotalPassengers(intSIZE).ToString & " passengers")

Hope that helps.

Cheers,

Ian
 
Back
Top