Airline Flight Code Visual Studio

3
AIRLINE FLIGHT Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' decare the destination and Departure collections Dim DepartureAirport As New Collection() Dim DestinationAirport As New Collection() ' this is a quick and dirty way of doing it and not ideal, but for a simple demo is OK ' populate with Airport codes (note i added the first empty string to satisfy one of you tests) DepartureAirport.Add("") DepartureAirport.Add("AUS") DepartureAirport.Add("BOS") DepartureAirport.Add("BWI") DepartureAirport.Add("DFW") DepartureAirport.Add("JFK") DepartureAirport.Add("LAX") DepartureAirport.Add("MIA") DepartureAirport.Add("ORD") DepartureAirport.Add("AUS") DepartureAirport.Add("SFO") DestinationAirport.Add("") DestinationAirport.Add("AUS") DestinationAirport.Add("BOS") DestinationAirport.Add("BWI") DestinationAirport.Add("DFW") DestinationAirport.Add("JFK") DestinationAirport.Add("LAX") DestinationAirport.Add("MIA") DestinationAirport.Add("ORD") DestinationAirport.Add("AUS") DestinationAirport.Add("SFO") ' wire up the ListBox1 to each collection ListBox1.DataSource = DepartureAirport ListBox2.DataSource = DestinationAirport End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ' if both Departure and Destination are the same If ListBox1.SelectedItem = ListBox2.SelectedItem Then MsgBox("Departure Airport is the same as the Destination Airport", MsgBoxStyle.Critical, "Error") Exit Sub End If ' if Departure in empty (hence the empt string added to the collection) If ListBox1.SelectedItem = "" Then

description

CHAPTER 4 PART 4.4 AN INTRODUCTION USING VISUAL BASIC 2012

Transcript of Airline Flight Code Visual Studio

Page 1: Airline Flight Code Visual Studio

AIRLINE FLIGHT

Public Class Form1

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

' decare the destination and Departure collections

Dim DepartureAirport As New Collection()

Dim DestinationAirport As New Collection()

' this is a quick and dirty way of doing it and not ideal, but for a simple demo is OK

' populate with Airport codes (note i added the first empty string to satisfy one of you tests)

DepartureAirport.Add("")

DepartureAirport.Add("AUS")

DepartureAirport.Add("BOS")

DepartureAirport.Add("BWI")

DepartureAirport.Add("DFW")

DepartureAirport.Add("JFK")

DepartureAirport.Add("LAX")

DepartureAirport.Add("MIA")

DepartureAirport.Add("ORD")

DepartureAirport.Add("AUS")

DepartureAirport.Add("SFO")

DestinationAirport.Add("")

DestinationAirport.Add("AUS")

DestinationAirport.Add("BOS")

DestinationAirport.Add("BWI")

DestinationAirport.Add("DFW")

DestinationAirport.Add("JFK")

DestinationAirport.Add("LAX")

DestinationAirport.Add("MIA")

DestinationAirport.Add("ORD")

DestinationAirport.Add("AUS")

DestinationAirport.Add("SFO")

' wire up the ListBox1 to each collection

ListBox1.DataSource = DepartureAirport

ListBox2.DataSource = DestinationAirport

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

' if both Departure and Destination are the same

If ListBox1.SelectedItem = ListBox2.SelectedItem Then

MsgBox("Departure Airport is the same as the Destination Airport", MsgBoxStyle.Critical, "Error")

Exit Sub

End If

' if Departure in empty (hence the empt string added to the collection)

If ListBox1.SelectedItem = "" Then

Page 2: Airline Flight Code Visual Studio

AIRLINE FLIGHT

MsgBox("Please select a Departure Airport", MsgBoxStyle.Critical, "Error")

Exit Sub

End If

' if Destination in empty (hence the empt string added to the collection)

If ListBox2.SelectedItem = "" Then

MsgBox("Please select a Destination Airport", MsgBoxStyle.Critical, "Error")

Exit Sub

End If

' if we get here then everything was good, book the flight

MsgBox("You are flying from " & ListBox1.SelectedItem & " to " & ListBox2.SelectedItem)

' hope you have a nice flight.....

End Sub

End Class

hope this gives you some ideas, for example, you could load the Airport codes in a better way..... good

luck...

Page 3: Airline Flight Code Visual Studio

AIRLINE FLIGHT

Here is the code I wrote for the same question, months ago.

It was selected the best answer.

I used a label as the booking control instead of a button. You can use a button by simply copying the

code in the label_click event to the button_click event.

Public Class Form1

Private Sub Label1_Click(sender As System.Object, e As System.EventArgs) Handles Label1.Click

If ListBox1.SelectedIndex >= 0 And ListBox2.SelectedIndex >= 0 _

And ListBox1.SelectedItem = ListBox2.SelectedItem Then

Label2.Text = "You have attempted to schedule a flight from and to the same destination, Change one of

the selections."

ElseIf ListBox1.SelectedIndex < 0 Or ListBox2.SelectedIndex < 0 Then

Label2.Text = "You have not selected one or both of the departure city or the destination city. Try again."

ElseIf ListBox1.SelectedIndex >= 0 And ListBox2.SelectedIndex >= 0 _

And ListBox1.SelectedItem <> ListBox2.SelectedItem Then

Label2.Text = " You have booked a flight from " & ListBox1.SelectedItem & " to " & ListBox2.SelectedItem

& "."

ListBox1.SelectedIndex = -1

ListBox2.SelectedIndex = -1

End If

End Sub

Private Sub ListBox1_GotFocus(sender As Object, e As System.EventArgs) Handles ListBox1.GotFocus

ListBox1.SelectedIndex = -1

ListBox2.SelectedIndex = -1

End Sub

End Class