Presentation is loading. Please wait.

Presentation is loading. Please wait.

14 Shipping Time App Using Dates and Timers

Similar presentations


Presentation on theme: "14 Shipping Time App Using Dates and Timers"— Presentation transcript:

1 14 Shipping Time App Using Dates and Timers
© by Pearson Education, Inc. All Rights Reserved.

2 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

3 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

4 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

5 Test-Driving the Shipping Time App
The default drop-off time (Fig. 14.1) is set to your computer’s current time when you execute the app. The time displayed in the Current time is: Label updates to the current time once each second. © by Pearson Education, Inc. All Rights Reserved.

6 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

7 14.2 Date Variables The primitive type Date simplifies manipulation, storage and display of date (and time) information. Date corresponds to the DateTime type in the .NET Framework Class Library. You use the New keyword when creating a Date value. In the code, the statement: © by Pearson Education, Inc. All Rights Reserved.

8 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

9 14.2 Date Variables (Cont.) The New keyword calls the Date’s constructor—a procedure that initializes an object when it’s created. © by Pearson Education, Inc. All Rights Reserved.

10 14.2 Date Variables (Cont.) The constructor used here requires six arguments—year, month, day, hour, minute and second—which are described in Fig © by Pearson Education, Inc. All Rights Reserved.

11 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

12 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

13 14.2 Date Variables (Cont.) Method overloading allows you to create multiple methods with the same name but different signatures. This means different numbers and types of parameters, or with parameters ordered differently (by type). When an overloaded method is called, the compiler selects the proper one by examining the number, types and order (by type) of the arguments. Often, method overloading is used to create several methods with the same name that perform similar tasks. If a type provides overloaded constructors or methods, Intellisense shows a tooltip (Fig. 14.3) containing one of the available overloads and you can cycle through the others by clicking the tooltip. © by Pearson Education, Inc. All Rights Reserved.

14 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

15 14.2 Date Variables (Cont.) After assigning a value to a Date variable, you can access its properties using the member- access (dot) operator, as follows: Dim year = delivery.Year ' retrieves Date delivery's year Dim month = delivery.Month ' retrieves Date delivery's month Dim day = delivery.Day ' retrieves Date delivery's day Dim hour = delivery.Hour ' retrieves Date delivery's hour Dim minute = delivery.Minute ' retrieves Date delivery's minute Dim second = delivery.Second ' retrieves Date delivery's second © by Pearson Education, Inc. All Rights Reserved.

16 14.2 Date Variables (Cont.) To add or subtract values in Date variables, you must call the correct method, using the member-access operator (Fig. 14.4). © by Pearson Education, Inc. All Rights Reserved.

17 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

18 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

19 14.3 Creating the Shipping Time App: Design Elements
When the Form loads: Set range of possible drop-off times to any time in the current day Call sub procedure DisplayDeliveryTime to determine and display the shipment’s delivery time When the user changes the drop-off time: After one second has elapsed: Update and display the current time When the DisplayDeliveryTime procedure gets called: Call function DepartureTime to determine the time the shipment’s flight departs © by Pearson Education, Inc. All Rights Reserved.

20 14.3 Creating the Shipping Time App: Design Elements (Cont.)
Add 3 hours to determine the delivery time (takes into account 6 hours for time of flight minus 3 hours for the time difference) Display the delivery time When the DepartureTime procedure gets called: Select correct Case based on the hour the shipment was dropped off Case where the drop-off hour is 0–10 (midnight to 10:59 AM) Delivery set to depart on noon flight of current day Case where the drop-off hour is 23 (11:00–11:59 PM) Delivery set to depart on noon flight of next day Case where none of the preceding Cases match Delivery set to depart on midnight flight of current day © by Pearson Education, Inc. All Rights Reserved.

21 Action/Control/Event (ACE) Table for the Shipping Time App
Use an ACE table to convert pseudocode into Visual Basic (Fig. 14.5). © by Pearson Education, Inc. All Rights Reserved.

22 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

23 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

24 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

25 Placing Controls in a GroupBox
Add a GroupBox named dropOffGroupBox by double clicking the GroupBox control in the Containers tab of the Toolbox. Change the Text property to Drop Off. Place the GroupBox above the provided GroupBox and make them the same size (Fig. 14.6). © by Pearson Education, Inc. All Rights Reserved.

26 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

27 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

28 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

29 Placing Controls in a GroupBox (Cont.)
Click the Label control in the Toolbox, then click inside the GroupBox that you just created. Change the Label’s Text property to Enter drop-off time: and its Name property to dropOffLabel. Then change the position of the Label by setting its Location property to 6, 33 (Fig. 14.7). The Location of a control is measured from the top-left corner of its container object. © by Pearson Education, Inc. All Rights Reserved.

30 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

31 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

32 Creating and Customizing the DateTimePicker
To add a DateTimePicker to your app, drag a DateTimePicker control from the Toolbox and drop it to the right of the Enter drop-off time: Label to place it in the GroupBox (Fig. 14.8). Change its Name property to dropOffDateTimePicker. Align the DateTimePicker with its descriptive Label. Next, change its Format property to Custom. © by Pearson Education, Inc. All Rights Reserved.

33 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

34 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

35 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

36 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

37 Creating and Customizing the DateTimePicker (Cont.)
When the DateTimePicker’s Format property is set to Custom, it uses the format that you specify in the CustomFormat property. Set the CustomFormat property to hh:mm tt. This property is case sensitive. The Format property eliminates the problem of a user’s entering a letter or symbol when the app expects a number. The DateTimePicker also prevents the user from specifying an invalid time, such as 32:15. © by Pearson Education, Inc. All Rights Reserved.

38 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

39 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

40 Creating and Customizing the DateTimePicker (Cont.)
Set the DateTimePicker’s ShowUpDown property to True. For this app, the user needs to enter only the time of day, so you use up and down arrows to allow the user to select the time (Fig. 14.9). © by Pearson Education, Inc. All Rights Reserved.

41 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

42 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

43 Creating a Timer Control
A Timer control is an object that can run code at a specified time interval in milliseconds (1/1000s of a second) by generating a Tick event. By default, the Timer runs code every 100 milliseconds. Each time the Tick event occurs, its event handler executes. Add a Timer to the Form by dragging and dropping the Timer control from the Toolbox’s Components tab. © by Pearson Education, Inc. All Rights Reserved.

44 Creating a Timer Control (Cont.)
Rename the Timer to clockTimer (Fig. 14.10). Set the Timer’s Enabled property to True, then set its Interval property to 1000, which specifies the number of milliseconds between Tick events. © by Pearson Education, Inc. All Rights Reserved.

45 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

46 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

47 Coding the Shipping Time Application’s Clock
Double click the Timer control in the component tray to generate the empty event handler for the Tick event (Fig. 14.11). The event handler formats its information to match the format you specify, "{hh:mm:ss tt}". © by Pearson Education, Inc. All Rights Reserved.

48 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

49 Using Code to Display a Delivery Time
To run code when the app first opens, create an event handler for the Form’s Load event (Fig. 14.12). Double click an empty area in the Form or the title bar to generate the Load event handler and enter Code view. Be careful not to double click a control on the Form; this generates the control’s event handler instead. © by Pearson Education, Inc. All Rights Reserved.

50 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

51 Using Code to Display a Delivery Time (Cont.)
These lines (Fig. 14.13) set the MinDate and MaxDate properties for dropOffDateTimePicker. © by Pearson Education, Inc. All Rights Reserved.

52 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

53 Using Code to Display a Delivery Time (Cont.)
The earliest drop-off time (MinDate) is set to 12:00 A.M (midnight) of the current day, and the latest drop-off time (MaxDate) is set to 12:00 AM the following day. The Date type also provides property Today, which returns the current date with the time set to 00:00:00 (midnight). © by Pearson Education, Inc. All Rights Reserved.

54 Using Code to Display a Delivery Time (Cont.)
The IDE underlines the call to DisplayDeliveryTime in blue due to the compilation error you introduce by calling a procedure that has not yet been written (Fig. 14.14). The DisplayDeliveryTime procedure calculates the delivery time in Las Vegas and displays the result in the Delivery time: Label. © by Pearson Education, Inc. All Rights Reserved.

55 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

56 Coding the ValueChanged Event Handler
Double click the DateTimePicker control dropOffDateTimePicker to generate its ValueChanged event handler (Fig. 14.15). © by Pearson Education, Inc. All Rights Reserved.

57 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

58 Coding the DisplayDeliveryTime Procedure
DepartureTime is underlined in blue in the IDE (Fig. 14.16) because the procedure has not yet been written. The DepartureTime procedure determines which flight (midnight or noon) the seafood shipment will use. A Date variable’s ToLongDateString method returns the date as a String in the format “Wednesday, October 30, 2012.” A Date variable’s ToShortTimeString returns the time as a String in the format “4:00 PM.” © by Pearson Education, Inc. All Rights Reserved.

59 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

60 Coding the DepartureTime Procedure
Line 50 (Fig. 14.17) stores the current date in the Date variable currentDate. Line 51 declares the Date variable departTime, the variable you use to store the DepartureTime Function procedure’s return value. © by Pearson Education, Inc. All Rights Reserved.

61 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

62 Coding the DepartureTime Procedure (Cont.)
The Select Case statement (Fig. 14.18) uses the hour specified by the user in the DateTimePicker as the controlling expression. The DateTimePicker’s Value property (which is of type Date) contains the value selected by the user. © by Pearson Education, Inc. All Rights Reserved.

63 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

64 Coding the DepartureTime Procedure (Cont.)
The first Case statement’s expression list determines whether the DateTimePicker’s value is between midnight and 10:59 AM (Hour = 10). If so, the seafood shipment takes the noon flight to Las Vegas. The next Case statement’s expression list determines whether the value in the DateTimePicker is between 11:00 PM and 11:59 PM (Hour = 23). If so, the seafood shipment takes the noon flight to Las Vegas the next day. © by Pearson Education, Inc. All Rights Reserved.

65 Coding the DepartureTime Procedure (Cont.)
The Case Else’s body executes if the controlling expression matches neither of the other two Cases (the value in the DateTimePicker is between 11:00 AM and 10:59 PM). In this case, the seafood shipment takes the midnight flight to Las Vegas. © by Pearson Education, Inc. All Rights Reserved.

66 Coding the DepartureTime Procedure (Cont.)
Line 68 (Fig. 14.19) returns the Date value containing the flight departure time. © by Pearson Education, Inc. All Rights Reserved.

67 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

68 Figure 14.20 presents the source code for the app.
Outline Figure 14.20 presents the source code for the app. © by Pearson Education, Inc. All Rights Reserved.

69 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

70 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

71 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.


Download ppt "14 Shipping Time App Using Dates and Timers"

Similar presentations


Ads by Google