Tutorial 14 – Shipping Time Application Using DateTimes and Timers Outline 14.1 Test-Driving the Shipping Time Application 14.2 DateTime Variables 14.3 Building the Shipping Time Application: Design Elements 14.4 Creating the Shipping Time Application: Inserting Code 14.5 Wrap-Up
In this tutorial, you will learn to: Objectives In this tutorial, you will learn to: Create and manipulate DateTime variables. Execute code at regular intervals using a Timer control. Retrieve DateTime input with a DateTimePicker control. Group controls using a GroupBox control.
14.1 Test-Driving the Shipping Time Application
14.1 Test-Driving the Shipping Time Application Figure 14.1 Shipping Time application. DateTimePicker with up-down arrows GroupBoxes
14.2 DateTime Variables Keyword new DateTime variable Calls a structure’s constructor Example: DateTime dtmDelivery = new DateTime( 2003, 1, 1, 0, 0, 0 ) Constructors initialize class objects or structure values DateTime variable Stores information about a point in time Contains properties Year, Month, Day, Hour, Minute, and Second
14.2 DateTime Variables Figure 14.2 DateTime constructor arguments.
14.2 DateTime Variables Figure 14.3 DateTime methods that perform various calculations.
14.3 Building the Shipping Time Application: Design Elements
14.3 Building the Shipping Time Application: Design Elements
14.3 Building the Shipping Time Application: Design Elements Figure 14.5 GroupBox controls on the Shipping Time Form. Newly created GroupBox displaying the text Drop Off GroupBoxes Add a second Groupbox to contain drop off time
14.3 Building the Shipping Time Application: Design Elements Figure 14.6 Adding a Label to a GroupBox. Before clicking inside the GroupBox Place a label inside the GroupBox by clicking the Label tab in the Toolbox
14.3 Building the Shipping Time Application: Design Elements Figure 14.7 DateTimePicker control on the Form. DateTimePicker control before modification (note that the appearance is similar to a ComboBox) Add a DateTimePicker to the application by clicking the DateTimePicker control in the Toolbox
14.3 Building the Shipping Time Application: Design Elements Figure 14.8 Customized DateTimePicker control on the Form. Up-down arrows for DateTimePicker (note that the appearance is similar to a NumericUpDown control) Set ShowUpDown property to true
14.3 Building the Shipping Time Application: Design Elements Figure 14.9 Timer control is displayed in the component tray. Component tray Timer control
14.3 Building the Shipping Time Application: Design Elements Figure 14.10 Rearranging and commenting the control declarations.
14.4 Creating the Shipping Time Application: Inserting Code Tick event handler DateTime property Now retrieves current time Format output hh:mm:ss tt
14.4 Creating the Shipping Time Application: Inserting Code Figure 14.11 Inserting code for a Tick event. Printing the current time
14.4 Creating the Shipping Time Application: Inserting Code Load event handler Date stored in dtmCurrentTime FrmShippingTime_Load event handler MinDate: earliest date allowed MaxDate: latest date allowed
14.4 Creating the Shipping Time Application: Inserting Code Figure 14.12 Storing the current time. Storing the current time in dtmCurrentTime
14.4 Creating the Shipping Time Application: Inserting Code MinDate property Earliest value user can enter Set to 12:00 AM MaxDate property Latest value user can enter Seafood must remain fresh, so value will be set to one day after MinDate
14.4 Creating the Shipping Time Application: Inserting Code Figure 14.13 Setting the MinDate and MaxDate properties. Setting the range of drop-off times
14.4 Creating the Shipping Time Application: Inserting Code Figure 14.14 Calling the DisplayDeliveryTime method. Displaying the delivery time
14.4 Creating the Shipping Time Application: Inserting Code Need to allow user to select a date Application will calculate delivery time ValueChanged event handler
14.4 Creating the Shipping Time Application: Inserting Code Figure 14.15 Inserting code in the ValueChanged event handler. Calculating and displaying the delivery time
14.4 Creating the Shipping Time Application: Inserting Code Figure 14.16 Calling the DepartureTime method. Determining the departure time Calculating the travel time Displaying the delivery time
14.4 Creating the Shipping Time Application: Inserting Code Figure 14.17 Inserting method DepartureTime into the application. Declaring variables
14.4 Creating the Shipping Time Application: Inserting Code Figure 14.18 Determining the seafood shipment’s flight departure time. Using the hour value stored in the DateTimePicker to determine departure time Noon departure time Noon (the next day) departure time Midnight departure time
14.4 Creating the Shipping Time Application: Inserting Code Figure 14.19 Returning the flight departure time. Returning the departure time
ShippingTime.cs (1 of 7)
ShippingTime.cs (2 of 7)
ShippingTime.cs (3 of 7)
ShippingTime.cs (4 of 7) Displaying the current time Setting the DateTimePicker’s minimum and maximum values
ShippingTime.cs (5 of 7) Calculating and displaying the delivery time in Las Vegas
ShippingTime.cs (6 of 7) Using a switch statement to determine departure time
ShippingTime.cs (7 of 7)