14 Shipping Time App Using Dates and Timers

Slides:



Advertisements
Similar presentations
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Advertisements

Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
VBA Modules, Functions, Variables, and Constants
Microsoft Visual Basic: Reloaded Chapter Eight Sub and Function Procedures.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and.
Variables and Constants
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Flag Quiz Application Introducing One-Dimensional Arrays and ComboBox es.
Chapter 4: The Selection Structure
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Menus,MonthCalender, DateTimePicker, MDI,Tree View, List View,
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Shipping Hub Application Introducing Generic Collections, LINQ, For Each...Next.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Wage Calculator Application.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Fund Raiser Application Introducing Scope, Pass-by-Reference and Option Strict.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Ticket Information Application Introducing Sequential-Access Files.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 5 Completing the Inventory Application Introducing Programming.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Microwave Oven Application Building Your Own Classes and Objects.
Applications Development
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Introduction to Visual Basic Programming. Introduction Simple Program: Printing a Line of Text Another Simple Program: Adding Integers Memory Concepts.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Inventory Application.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Controls Part 2. DateTimePicker Control Used for representing Date/Time information and take it as input from user. Date information is automatically.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Visual Basic.NET Programming for the Rest of Us Keith Mulbery Utah Valley State College.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
REEM ALMOTIRI Information Technology Department Majmaah University.
.NET MCQs MULTIPLE CHOICE QUESTION
Visual Basic.NET Windows Programming
Tutorial 14 – Shipping Time Application Using DateTimes and Timers
A variable is a name for a value stored in memory.
Chapter 2: The Visual Studio .NET Development Environment
Variables and Arithmetic Operations
13 Enhancing the Wage Calculator App
CHAPTER FIVE Decision Structures.
Using Procedures and Exception Handling
Chapter 2 Visual Basic Interface
CHAPTER FIVE Decision Structures.
Variables and Arithmetic Operations
Chapter 3 Introduction to Classes, Objects Methods and Strings
Tutorial 12 – Security Panel Application Introducing the Select Case Multiple-Selection Statement Outline Test-Driving the Security Panel Application.
Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects Outline Test-Driving the Microwave Oven Application Designing.
Chapter 3: Introduction to Problem Solving and Control Statements
T. Jumana Abu Shmais – AOU - Riyadh
CIS 16 Application Development Programming with Visual Basic
Tutorial 9 Sequential Access Files and Printing
Additional Topics in VB.NET
Presentation transcript:

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

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

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

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

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. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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: ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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

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

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

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

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. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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

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 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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

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

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

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). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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

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

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. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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

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. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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

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

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

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. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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

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). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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

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. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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

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}". ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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

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

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). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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

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

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.” ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

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

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

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

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

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

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