Tutorial 8: Example Use of a twodimensional Array (taken fromProgramming in Visual Basic .NET, by Bradley and Millspaugh) 1) You are going to create a project that looks up the driving distance between two cities. Use two dropdown lists that contain the names of the cities as indicated in the table below. Label one list “Departure” and the other “Destination. Use a button click event to calculate the distance. Use a two dimensional array to store the distances. 2) Your GUI will look as follows: 3) Table of driving distances follows: Boston Chicago Dallas Las Los Miami New Tor Van D.C Vegas Angeles Orleans onto cover Boston 0 1004 1753 2752 3017 1520 1507 609 3155 448 Chicago 1004 0 921 1780 2048 1397 919 515 2176 709 Dallas 1753 921 0 1230 1399 1343 517 1435 2234 1307 Las 2752 1780 1230 0 272 2570 1732 2251 1322 2420 Vegas Los 3017 2048 1399 272 0 2716 1858 2523 1278 2646 Angeles Miami 1520 1397 1343 2570 2716 0 860 1494 3447 1057 New 1507 919 517 1732 1858 860 0 1307 2734 1099Orleans Tor 609 515 1435 2251 2523 1494 1307 0 2820 571 onto Van 3155 2176 2234 1322 1278 3447 2734 2820 0 2887 cover D.C. 448 709 1307 2420 2646 1057 1099 571 2887 0 4) Properties Plan: Properties Plan for Tutorial 8 Object Property Setting Form1 Name Form1 Text Driving Distance AcceptButton btnLookUp Label1 Text Departure: Label2 Text ...
Tutorial 8:Example Use of a twodimensional Array (taken fromProgramming in Visual Basic .NET, by Bradley and Millspaugh)
1) You are going to create a project that looks up the driving distance between two cities. Use two dropdown lists that contain the names of the cities as indicated in the table below. Labelone list “Departure” and the other “Destination.Use a button click event to calculate the distance.Use a two dimensional array to store the distances.
2) Your GUI will look as follows:
3) Table of driving distances follows: Boston Chicago Dallas LasLos MiamiNew TorVan D.C Vegas AngelesOrleans ontocover Boston 01004 17532752 30171520 1507609 3155448 Chicago 10040 9211780 20481397 919515 2176709 Dallas 1753 9210 12301399 1343517 14352234 1307 Las 27521780 12300 2722570 17322251 13222420 Vegas Los 30172048 1399272 02716 18582523 12782646 Angeles Miami 15201397 13432570 27160 8601494 34471057 New 1507919 5171732 1858860 01307 27341099
Setting Form1 Driving Distance btnLookUp Departure: Destination: cboRow Boston Chicago Dallas Las Vegas Los Angeles Miami New Orleans Toronto Vancover Washington, DC DropDownList cboCol Boston Chicago Dallas Las Vegas Los Angeles Miami New Orleans Toronto Vancover Washington, DC DropDownList Distance in Miles: lblDistance
Property Name Text AcceptButton Text Text Name Items
3447 27342820 0
cboCol
4) Properties Plan: Properties Plan for Tutorial 8
btnLookUp &Distance btnClear C&lear btnExit E&xit
DropDownStyle Text Name Text Name Text Name Text Name Text
2887
DropDownStyle Name Items
Object Form1
5) Code ' Description: Look up the driving distance in miles between ' adeparture city and a destination city.Make use of ' twodimensional array that acts as a lookup table. ' Public ClassForm1 InheritsSystem.Windows.Forms.Form
Private SubbtnLookUp_Click(ByValsenderAsSystem.Object,ByVale AsSystem.EventArgs)HandlesbtnLookUp.Click ' Look up driving distances between two selected cities ' Departure city selected from the departure combo box ' determines the row index into the distanceInteger array ' declared as a modulelevel variable.The destination ' city selected from the destination combo box determines ' the column index into the distanceInteger array.Both ' are used as indexes to determine the correct look up ' value.
IfdepartureIndexInteger <> 1AnddestinationIndexInteger <> 1Then lblDistance.Text = distanceInteger(departureIndexInteger, destinationIndexInteger).ToString("N") Else MessageBox.Show("Select the destination and departure city.","Information Missing", _ MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End If End Sub
' Remove the selection from the departure and destination ' comboboxes and clear the driving distance cboRow.SelectedIndex = 1 cboColumn.SelectedIndex = 1 lblDistance.Text ="" End Sub Private SubbtnExit_Click(ByValsenderAsSystem.Object,ByValeAs System.EventArgs)HandlesbtnExit.Click ' Terminate the program Me.Close() End Sub End Class