Presentation is loading. Please wait.

Presentation is loading. Please wait.

T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 20 Shipping Hub Application Introducing Generic Collections, LINQ, For Each...Next.

Similar presentations


Presentation on theme: "T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 20 Shipping Hub Application Introducing Generic Collections, LINQ, For Each...Next."— Presentation transcript:

1 T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 20 Shipping Hub Application Introducing Generic Collections, LINQ, For Each...Next and Access Keys

2  2009 Pearson Education, Inc. All rights reserved. 2 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

3  2009 Pearson Education, Inc. All rights reserved. 3 Outline 20.6 Shipping Hub Application: Using Class List ( Of T ) 20.7 For Each...Next Repetition Statement 20.8 Language-Integrated Query (LINQ)

4  2009 Pearson Education, Inc. All rights reserved. 4 In this tutorial you will learn: ■Use generic collections. ■Create and manipulate a List(Of T) object. ■Use Language Integrated Query (LINQ) to select elements from a collection. ■Set the MaxLength property of a TextBox. Objectives

5  2009 Pearson Education, Inc. All rights reserved. 5 In this tutorial you will learn: ■Specify the tab order in a GUI using the TabStop and TabIndex properties of the controls. ■Create an access key for a control. ■Use a For Each...Next loop to iterate through a collection. ■Obtain a String representation of an object. Objectives

6 Application Requirements  2009 Pearson Education, Inc. All rights reserved. 6 20.1 Test-Driving the Shipping Hub Application A shipping company receives packages at its headquarters, which functions as its shipping hub. After receiving the packages, the company ships them to one of the following states: Alabama, Florida, Georgia, Kentucky, Mississippi, North Carolina, South Carolina, Tennessee, West Virginia or Virginia. The company needs an application to track the packages that pass through its shipping hub. For each package that arrives at the hub, the user clicks the application’s Scan New Button, to generate a package ID number. Once a package has been scanned, the user should be able to enter the shipping address for it. The user should be able to navigate through the list of scanned packages by using Button s and by viewing a list of all packages destined for a particular state.

7  2009 Pearson Education, Inc. All rights reserved. 7 Test-Driving the Shipping Hub Application ■Run the completed application (Fig. 20.1). Figure 20.1 | Shipping Hub application when first run.

8  2009 Pearson Education, Inc. All rights reserved. 8 Test-Driving the Shipping Hub Application (Cont.) ■Click the Scan New Button. ■The application displays a package ID number and the arrival time, enables the TextBox es and allows the user to enter the package information (Fig. 20.2). Figure 20.2 | Scanning a new package.

9  2009 Pearson Education, Inc. All rights reserved. 9 ■Type 318 Some Street in the Address: TextBox, then press the Tab key. Note that the cursor moves to the City: TextBox (Fig. 20.3). ■Fill in other input values, then click the Add Button to add the package to the application’s List. Test-Driving the Shipping Hub Application (Cont.) Figure 20.3 | Pressing the Tab key moves the cursor to the next TextBox. Cursor now appears in the City: TextBox

10  2009 Pearson Education, Inc. All rights reserved. 10 GUI Design Tip Using the Tab key is an efficient way for users to navigate through the controls in a GUI.

11  2009 Pearson Education, Inc. All rights reserved. 11 ■The application’s NEXT > and < BACK Button s allow the user to navigate the list of packages. ■The user can click on the Remove Button to delete packages and on the Edit Button to update a particular package’s information. ■The ComboBox on the right side of the application allows the user to select a state. –When a state is selected, all of the package ID numbers of packages destined for that state are displayed in the ListBox (Fig. 20.4). Test-Driving the Shipping Hub Application (Cont.)

12  2009 Pearson Education, Inc. All rights reserved. 12 Figure 20.4 | Viewing all packages going to South Carolina. Test-Driving the Shipping Hub Application (Cont.)

13  2009 Pearson Education, Inc. All rights reserved. 13 ■The table in Fig. 20.5 describes the properties for class Package. Figure 20.5 | Properties of class Package. 20.2 Package Class

14  2009 Pearson Education, Inc. All rights reserved. 14 ■Open the template application. ■In the Solution Explorer, right click the ShippingHub project. –Select Add > Existing Item.... –When the Add Existing Item dialog appears, select the Package.vb file and click Add (Fig 20.6). Adding a Class to an Application Figure 20.6 | Solution Explorer with Package.vb added. Package class added to the ShippingHub project

15  2009 Pearson Education, Inc. All rights reserved. 15 ■It is awkward for users to have to select each TextBox. ■Most applications allow the user to press the Tab key to navigate between the controls on the Form. ■The TabIndex property allows you to specify the order in which focus transfers between controls. –Some controls, such as a read-only TextBox, should not be selected using the Tab key. –The TabStop property specifies whether the user can select the control using the Tab key. 20.3 Using Properties TabIndex and TabStop

16  2009 Pearson Education, Inc. All rights reserved. 16 ■The TabStop property defaults to True for controls that receive user input. ■The IDE provides a view called Tab Order. –Select the Form, then select View > Tab Order (Fig. 20.7). Setting Properties TabIndex and TabStop Figure 20.7 | Setting the TabIndex properties using the Tab Order view of the Shipping Hub application. TabIndex box set to zero TabIndex boxes (not modified)

17  2009 Pearson Education, Inc. All rights reserved. 17 ■Click the Package Information GroupBox, and note that its value becomes 0. Then click the Address : TextBox. –Now the value changes to 0.0. The first zero refers to the TabIndex of the container, and the second zero refers to the TabIndex for that control within the container. Setting Properties TabIndex and TabStop (Cont.)

18  2009 Pearson Education, Inc. All rights reserved. 18 GUI Design Tip Use the TabIndex property to define the logical order in which the user should enter data. Usually the order transfers the focus of the application from top to bottom and left to right.

19  2009 Pearson Education, Inc. All rights reserved. 19 ■Continue setting the TabIndex properties by clicking the Scan New Button. Then click the remaining unchanged controls in the order indicated in Fig. 20.8. ■Exit the Tab Order view by selecting View > Tab Order or by pressing the Esc key. Figure 20.8 | Tab Order view of the Shipping Hub application. Setting Properties TabIndex and TabStop (Cont.)

20  2009 Pearson Education, Inc. All rights reserved. 20 ■Access keys (or keyboard shortcuts) allow the user to perform an action on a control using the keyboard. –Insert an & symbol in the control’s Text property before the letter you wish to use as an access key. ■To display an ampersand character on a control, type &&. 20.4 Using Access Keys

21  2009 Pearson Education, Inc. All rights reserved. 21 ■Insert an & symbol before the letter S in the Text property of the Scan New Button (Fig. 20.9). ■If the user presses Alt, then S, this has the same effect as “clicking” the Scan New Button. ■Create access keys for the remaining Button s. ■The access key does not have to be the first letter. Figure 20.9 | Creating an access key. Creating Access Keys Using the & symbol to create an access key (there is no space between & and S) Access key letters underlined (may need to press the Alt key first)

22  2009 Pearson Education, Inc. All rights reserved. 22 GUI Design Tip Use access keys to allow users to “click” a control using the keyboard.

23  2009 Pearson Education, Inc. All rights reserved. 23 ■The.NET Framework Class Library provides several collections. –These classes provide methods that facilitate the storage and organization of your data. –Collections simplify the details of how the objects are being stored. ■ List(Of T) provides a convenient alternative to arrays. –The identifier T is a placeholder which you replace with an actual type. Dim list As List(Of Integer) Dim list2 As List(Of Package) –Dynamic resizing enables the List object to change its size. 20.5 Collections

24  2009 Pearson Education, Inc. All rights reserved. 24 Software Design Tip Use a List to store a group of values when the number of elements in the group varies during the running of an application.

25  2009 Pearson Education, Inc. All rights reserved. 25 When the Form loads: Generate a random initial package ID number and arrival time Set the SelectedIndex of the State ComboBox to 0 Create an empty List of Packages When the user clicks the Scan New Button: Generate the next package ID number Create a new Package object Display the new Package’s package number and arrival time Enable the TextBoxes, the ComboBox and the Add Button 20.6 Shipping Hub Application: Using Class List(Of T)

26  2009 Pearson Education, Inc. All rights reserved. 26 When the user clicks the Add Button: Retrieve address, city, state and zip code values; and disable input controls Add the package to the List Enable the Package Information GroupBox and the appropriate Buttons Disable the Add Button Add the package number to the ListBox Change the Packages by Destination ComboBox value to the package’s destination state Enable the New Button 20.6 Shipping Hub Application: Using Class List(Of T) (Cont.)

27  2009 Pearson Education, Inc. All rights reserved. 27 When the user clicks the < BACK Button: Display the previous package in the List When the user clicks the NEXT > Button: Display the next package in the List When the user clicks the Remove Button: Remove the package from the Packages by Destination ListBox Remove the package from the List 20.6 Shipping Hub Application: Using Class List(Of T) (Cont.)

28  2009 Pearson Education, Inc. All rights reserved. 28 When the user clicks the Edit Button: Change the Button to read Update Allow the user to modify package address information When the user clicks the Update Button: Update the package’s information in the List Disable controls that allow user input, and change the Update Button to read Edit When the user chooses a different state in the Packages by Destination ComboBox: Display the package number for each package destined for that state in the ListBox 20.6 Shipping Hub Application: Using Class List(Of T) (Cont.)

29  2009 Pearson Education, Inc. All rights reserved. 29 ■Use an ACE table to convert the pseudocode to Visual Basic (Fig. 20.10). Figure 20.10 | ACE table for the Shipping Hub application. (Part 1 of 5.) Action/Control/Event (ACE) Table for the Shipping Hub Application

30  2009 Pearson Education, Inc. All rights reserved. 30 Figure 20.10 | ACE table for the Shipping Hub application. (Part 2 of 5.) Action/Control/Event (ACE) Table for the Shipping Hub Application (Cont.)

31  2009 Pearson Education, Inc. All rights reserved. 31 Figure 20.10 | ACE table for the Shipping Hub application. (Part 3 of 5.) Action/Control/Event (ACE) Table for the Shipping Hub Application (Cont.)

32  2009 Pearson Education, Inc. All rights reserved. 32 Figure 20.10 | ACE table for the Shipping Hub application. (Part 4 of 5.) Action/Control/Event (ACE) Table for the Shipping Hub Application (Cont.)

33  2009 Pearson Education, Inc. All rights reserved. 33 Figure 20.10 | ACE table for the Shipping Hub application. (Part 5 of 5.) Action/Control/Event (ACE) Table for the Shipping Hub Application (Cont.)

34  2009 Pearson Education, Inc. All rights reserved. 34 ■Declare a List(Of Package) list (Fig. 20.11). Figure 20.11 | Declaring the List(Of Package) reference. Creating a List of Packages Declaring a List(Of Package) reference

35  2009 Pearson Education, Inc. All rights reserved. 35 ■Assign the reference of a new List(Of Package) to the instance variable list (Fig. 20.12). Figure 20.12 | Creating a List(Of Package) object. Creating a List of Packages (Cont.) Initializing the List(Of Package) reference

36  2009 Pearson Education, Inc. All rights reserved. 36 ■The user clicks the Scan New Button when a new package arrives at the shipping hub. ■Line 28 (Fig. 20.13) increments packageID to ensure that all packages have a unique identification number. Figure 20.13 | Creating a Package object. Adding and Removing Packages Create a new Package object with a unique ID

37  2009 Pearson Education, Inc. All rights reserved. 37 ■The application should display a scanned package’s arrival time and package number (Fig. 20.14). ■The ToString method returns a String representation of an object. – Date ’s ToString method returns the date as a String. –For some.NET classes, ToString merely returns the class name. Adding and Removing Packages (Cont.) Displaying arrival time and package ID number in Labels Figure 20.14 | Displaying the package’s number and arrival time.

38  2009 Pearson Education, Inc. All rights reserved. 38 ■This code (Fig. 20.15) stores the package information by adding the Package object to list using the List ’s Add method. ■New Package s are added to the end of the List. Figure 20.15 | Adding a package to the List. Adding a Package object to a List Adding and Removing Packages (Cont.)

39  2009 Pearson Education, Inc. All rights reserved. 39 ■An element’s location in the List is the element’s index. ■The List class provides a simple way to remove elements from the List. –The RemoveAt method removes a package from the List (Fig. 20.16). –The argument passed to the method RemoveAt is the index of the Package in the List. Adding and Removing Packages (Cont.) Removing the current Package from the List Figure 20.16 | Removing a Package from the List.

40  2009 Pearson Education, Inc. All rights reserved. 40 ■If a Package at index 3 is removed from the List, the Package that was previously at index 4 will then be located at index 3. ■The Count property returns the number of elements currently stored in the List. Adding and Removing Packages (Cont.)

41  2009 Pearson Education, Inc. All rights reserved. 41 ■When the Edit Button is clicked, line 135 (Fig. 20.17) changes the text on the Edit Button to &Update. ■This indicates that the user should click the same Button to submit changes to the package information. Figure 20.17 | Changing the Edit Button to display Update. Using code to change the text displayed on a Button Updating Package Information

42  2009 Pearson Education, Inc. All rights reserved. 42 ■When the user chooses to alter the package information, the Package is removed from the List and a new one with the updated address information is added (Fig. 20.18). Figure 20.18 | Removing and inserting a Package to update data. Updating the List with new package information Updating Package Information (Cont.) ■The Insert method allows you to specify the index in the List at which to insert the Package.

43  2009 Pearson Education, Inc. All rights reserved. 43 ■After the user clicks the Update Button, the TextBox es are disabled, and the text on the Update Button is reset to Edit (Fig. 20.19). Figure 20.19 | Setting the Button ’s Text property back to Edit. Using code to display the text on the Button Updating Package Information (Cont.)

44  2009 Pearson Education, Inc. All rights reserved. 44 ■Retrieve the data from the List using the elements index enclosed in parentheses (Fig. 20.20). Figure 20.20 | Retrieving a Package from the List. Retrieving a Package object from a List Displaying a Package

45  2009 Pearson Education, Inc. All rights reserved. 45 ■These lines retrieve the package information from packageObject and display the data in the corresponding controls on the Form (Fig. 20.21). Figure 20.21 | Displaying the package data in the Form ’s controls. Displaying data stored in the Package object Displaying a Package (Cont.)

46  2009 Pearson Education, Inc. All rights reserved. 46 ■The For Each...Next repetition statement iterates through all the elements in an array or a collection. ■The statement uses a control variable that can be assigned each element in the collection: For Each packageObject As Package In list packagesListBox.Items.Add(packageObject. PackageNumber) Next 20.7 For Each...Next Repetition Statement

47  2009 Pearson Education, Inc. All rights reserved. 47 ■The statement requires both a group and an element. –The group specifies the collection through which you wish to iterate. –The element is used to store a reference to an object in the group. –The statement assigns the current element in the collection to the element variable. 20.7 For Each...Next Repetition Statement (Cont.)

48  2009 Pearson Education, Inc. All rights reserved. 48 Good Programming Practice Use a For Each...Next repetition statement to iterate through values in an array or collection without using a counter variable.

49  2009 Pearson Education, Inc. All rights reserved. 49 Common Programming Error If the element in a For Each...Next statement cannot be converted to the same type as the groups’s objects, a compilation error occurs. For example, if a List contained Date values, declaring a reference to a Package object as the element would cause a compilation error.

50  2009 Pearson Education, Inc. All rights reserved. 50 ■Figure 20.22 shows the UML activity diagram for the For Each...Next statement. Figure 20.22 | UML activity diagram for For Each...Next repetition statement. 20.7 For Each...Next Repetition Statement (Cont.)

51  2009 Pearson Education, Inc. All rights reserved. 51 ■This loop (Fig. 20.23) iterates through list. ■This code is added to a ComboBox ’s SelectedIndexChanged event handler. –The SelectedIndexChanged event occurs when the value selected in the ComboBox changes. Inserting a For Each...Next Statement For Each...Next header Figure 20.23 | Writing a For Each...Next statement.

52  2009 Pearson Education, Inc. All rights reserved. 52 ■These lines contain an If...Then statement (Fig. 20.24) that tests each package’s destination state against the state name displayed in the Packages by Destination ComboBox. Figure 20.24 | Displaying all packages going to selected state. Inserting a For Each...Next Statement (Cont.) Displaying package ID numbers only for packages destined for the specified state

53  2009 Pearson Education, Inc. All rights reserved. 53 ■Visual Basic 2008 and the.NET 3.5 framework introduce Language-Integrated Query (LINQ). –A query retrieves specific information from a data source, such as a collection. ■A typical LINQ query contains three clauses: –The From clause specifies a range variable and the data source to query. –The Where clause specifies the conditions that must be met for the item to be included in the results. –The Select clause specifies the value(s) placed in the results. 20.8 Language-Integrated Query (LINQ)

54  2009 Pearson Education, Inc. All rights reserved. 54 ■The following LINQ to Objects query selects all the Package s in list that are destined for Boston: Dim cityQuery = From p In list _ Where p.City = "Boston" _ Select p ■If the expression in the Where clause evaluates to True, the Select clause includes the corresponding Package in the result. ■LINQ uses deferred execution—the query does not execute until you attempt to iterate through the query results. 20.8 Language-Integrated Query (LINQ) (Cont.)

55  2009 Pearson Education, Inc. All rights reserved. 55 ■Lines 222–225 of Figure 20.25 declare a LINQ query that selects from list all the Package s destined for the specified state. Figure 20.25 | Declaring a LINQ query. Using LINQ to Select Package s from a List Declaring a LINQ query to select Packages destined for specified state

56  2009 Pearson Education, Inc. All rights reserved. 56 ■The query results are contained in an IEnumerable(Of Package) object. – IEnumerable is an interface—a set of methods that can be called on an object to tell the object to perform some task or return some piece of information. –You can call any IEnumerable method on an array or collection object to iterate through its elements. Using LINQ to Select Package s from a List (Cont.)

57  2009 Pearson Education, Inc. All rights reserved. 57 Figure 20.26 | Iterating through query results using a For Each...Next statement. Use a For Each...Next statement to display the results of the query Using LINQ to Select Package s from a List (Cont.) ■Replace the previous Package-selection code with lines 227-231 of Fig. 20.26.

58  2009 Pearson Education, Inc. All rights reserved. 58 ■Figure 20.27 presents the source code for the Shipping Hub application. Outline (1 of 12 ) Declares a List to be used to store packages Initially, there are no objects in the List, so set the position to zero Use a Random object to generate a random number for package IDs Create a List object

59  2009 Pearson Education, Inc. All rights reserved. 59 Outline (2 of 12 ) Use ToString to convert values

60  2009 Pearson Education, Inc. All rights reserved. 60 Outline (3 of 12 )

61  2009 Pearson Education, Inc. All rights reserved. 61 Outline (4 of 12 ) When the user clicks the < BACK Button, decrement the position. If the position was zero, set the position to the last object in the List

62  2009 Pearson Education, Inc. All rights reserved. 62 Outline (5 of 12 ) 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

63  2009 Pearson Education, Inc. All rights reserved. 63 Outline (6 of 12 ) Set the position to the next package in the List

64  2009 Pearson Education, Inc. All rights reserved. 64 Outline (7 of 12 ) Using & in the Text property of a Button to create an access key Removing and inserting items from/into a List

65  2009 Pearson Education, Inc. All rights reserved. 65 Outline (8 of 12 ) Using & in the Text property of a Button to create an access key Retrieve data from user, and store it in the Package object

66  2009 Pearson Education, Inc. All rights reserved. 66 Outline (9 of 12 ) Using ToString to convert values

67  2009 Pearson Education, Inc. All rights reserved. 67 Outline (10 of 12 ) Enable or disable Button s depending on value of state

68  2009 Pearson Education, Inc. All rights reserved. 68 Outline (11 of 12 )

69  2009 Pearson Education, Inc. All rights reserved. 69 Outline (12 of 12 ) Declare a LINQ query to select Packages destined for specified state


Download ppt "T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 20 Shipping Hub Application Introducing Generic Collections, LINQ, For Each...Next."

Similar presentations


Ads by Google