© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Shipping Hub Application 20.2 Package Class 20.3 Using Properties TabIndex and TabStop 20.4 Using Access Keys 20.5 Collections 20.6 Shipping Hub Application: Using Class ArrayList 20.7 foreach Repetition Statement 20.8 Wrap-Up Tutorial 20 – Shipping Hub Application Introducing Collections, the foreach Statement and Access Keys
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Create and manipulate an ArrayList object. –Set the TabStop and TabIndex properties of a control. –Create an access key for a control. –Use a foreach loop to iterate through an ArrayList.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Shipping Hub Application
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Shipping Hub Application Figure 20.1 Shipping Hub application when first run. Run the Shipping Hub application – Debug > Start
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Shipping Hub Application Figure 20.2 Scanning a new package. Click Scan New Button Click in Address: TextBox and type 318 Some Street
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Shipping Hub Application Figure 20.3 Pressing the Tab key moves the cursor to the next TextBox. Cursor now appears in the City : TextBox
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Shipping Hub Application Figure 20.4 Viewing all packages going to South Carolina. You can view all Package ID’s by state using the ComboBox
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Package Class
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Package Class Figure 20.6 Solution Explorer with Package.cs added. Package class added to the ShippingHub project
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Properties TabIndex and TabStop TabIndex and access keys – Requires only the keyboard (allows user to avoid constantly switching between using the mouse and keyboard).
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Properties TabIndex and TabStop Figure 20.7 Setting the TabIndex properties using the Tab Order view of the Shipping Hub application. TabIndex boxes (not modified) TabIndex box set to zero Set TabStop property to true for controls that receive user input and false for the other controls
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Properties TabIndex and TabStop Figure 20.8 Tab Order view of the Shipping Hub application. Set the TabIndex properties by clicking each control in the order indicated
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Access Keys Figure 20.9 Creating an access key. Using the & symbol to create an access key (there is no space between & and S ) Access key letter underlined
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Collections Collections –Used to store groups of related objects –Aid in storage and organization of data, requiring no knowledge of how they work. ArrayList –Dynamic resizing capability Grow and shrink according to storage needs –Contained in Collections namespace
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Shipping Hub Application:Using Class ArrayList
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Shipping Hub Application:Using Class ArrayList
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Shipping Hub Application:Using Class ArrayList
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Shipping Hub Application:Using Class ArrayList Figure Declaring the ArrayList reference. Declaring an ArrayList reference Accessing ArrayList – Use the member-access operator (. ) OR – Member of System.Collections namespace
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Shipping Hub Application:Using Class ArrayList Figure Creating an ArrayList object. Creating the ArrayList reference
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Shipping Hub Application:Using Class ArrayList Figure Creating a Package object. Create a new Package object with a unique ID
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Shipping Hub Application:Using Class ArrayList Figure Displaying the package’s number and arrival time. Displaying arrival time and package ID number in Label s ToString method returns value representing object as a string – Example: 11/11/2002 9:34:00 AM
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Shipping Hub Application:Using Class ArrayList Figure Adding a package to the ArrayList. Adding a Package object to an ArrayList An Object’s position in ArrayList is called an index – Index of first object is zero
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Shipping Hub Application:Using Class ArrayList Figure Removing a package from the ArrayList. Removing the current package from the ArrayList
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Shipping Hub Application:Using Class ArrayList Figure Changing the Edit Button to display Update. Using code to change the text displayed on a Button
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Shipping Hub Application:Using Class ArrayList Figure Removing and inserting a package to update data. Updating the ArrayList with new package information
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Shipping Hub Application:Using Class ArrayList Figure Setting the Button ’s Text property back to Edit. Using code to display the text on the Button
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Shipping Hub Application:Using Class ArrayList Figure Retrieving a package from the ArrayList. Retrieving a Package object from an ArrayList
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Shipping Hub Application:Using Class ArrayList Figure Displaying the package data in the Form ’s controls. Displaying data stored in the Package object
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved foreach Repetition Statement foreach ( Package objPackage in objArrayList ) { lstPackages.Items.Add( objPackage.PackageNumber ); } The foreach statement requires both a collection type and an element – Collection type : specifies the array or collection through which you wish to iterate – Element : Use to store a reference to a value in the collection type
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved foreach Repetition Statement Figure UML activity diagram for foreach repetition statement.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved foreach Repetition Statement Figure Writing a foreach statement. foreach header
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved foreach Repetition Statement Figure Displaying all packages going to selected state. Displaying package ID numbers only for packages destined for the specified state
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 33 ShippingHub.cs (1 of 15)
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 34 ShippingHub.cs (2 of 15)
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 35 ShippingHub.cs (3 of 15)
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 36 ShippingHub.cs (4 of 15)
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 37 ShippingHub.cs (5 of 15) Use a Random object to generate a random number for package IDs Initially, there are no objects in the ArrayList so set the position to zero
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 38 ShippingHub.cs (6 of 15)
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 39 ShippingHub.cs (7 of 15) Display package ID numbers in ListBox if the state names match
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 40 ShippingHub.cs (8 of 15) When the user clicks the NEXT > Button, increment the position. If the position was the last object in the array, set the position to zero When the user clicks the < BACK Button, decrement the position. If the position was zero, set the position to the last object in the ArrayList
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 41 ShippingHub.cs (9 of 15) Load the next package, update m_intPosition as necessary
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 42 ShippingHub.cs (10 of 15)
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 43 ShippingHub.cs (11 of 15)
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 44 ShippingHub.cs (12 of 15) Retrieve data from user, and store it in the Package object
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 45 ShippingHub.cs (13 of 15) Enable or disable Buttons depending on value of blnState
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 46 ShippingHub.cs (14 of 15) Defining the SelectedIndexChanged event of a ComboBox
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 47 ShippingHub.cs (15 of 15)