310 likes | 402 Views
Tutorial 14 – Shipping Time Application Using Date s and Timer s. Outline 14.1 Test-Driving the Shipping Time Application 14.2 Date Variables 14.3 Building the Shipping Time Application: Design Elements 14.4 Creating the Shipping Time Application: Inserting Code 14.5 Wrap-Up.
E N D
Tutorial 14 – Shipping Time ApplicationUsing Dates and Timers Outline 14.1 Test-Driving the Shipping Time Application14.2 Date Variables14.3 Building the Shipping Time Application: Design Elements14.4 Creating the Shipping Time Application: Inserting Code14.5 Wrap-Up
Objectives • In this tutorial, you will learn to: • Create and manipulate Date variables. • Execute code at regular intervals using a Timer control. • Retrieve Date input with a DateTimePicker control. • Group controls using a GroupBox control.
DateTimePickerwith up-down arrows GroupBoxes 14.1 Test-Driving the Shipping Time Application Figure 14.1 Shipping Time application.
14.2 Date Variables • Keyword New • Calls a structure’s constructor • Example: New Date(2003, 1, 1, 0, 0, 0) • Constructors initialize class objects or structure values • Datevariable • Stores information about a point in time • Contains properties Year, Month, Day, Hour, Minute, and Second
14.2 Date Variables Figure 14.2 Date constructor arguments.
Visual Basic .NET statement Result Assume has been initialized with a value. dtmDelivery Date dtmDelivery = dtmDelivery.AddHours( 3 ) Add hours. 3 dtmDelivery = dtmDelivery.AddMinutes( - 5 ) Subtract minutes. 5 dtmDelivery = dtmDelivery.AddDays( 1 ) Add day . 1 dtmDelivery = dtmDelivery.AddMinutes( 30 ) Add minutes. 30 dtmDelivery = dtmDelivery.AddHours( - 12 ) Subtract hours. 12 14.2 Date Variables Figure 14.3 Date methods that perform various calculations.
Action Control Event/Method Application is run Label the application’s controls lblCurrentTime , lblDropOff , lblDeliveryTime FrmShippingTime Load Set range of possible drop off times dtpDropOff to any time in the current day Determine the shipment’s d elivery dtpDropOff time Display the shipment’s delivery lblLasVegasTime time dtpDropOff ValueChanged Determine the shipment’s delivery dtpDropOff time Display the shipment’s delivery lblLasVegasTime time tmrClock Tick Update and display the cur rent lblCurrentTime time DisplayDeliveryTime Determine the time the shipment’s dtpDropOff flight will depart Add three hours to determine the delivery time Display the delivery time lblLasVegasTime Figure 14.4 ACE table for the ap plication. Shipping Time 14.3 Building the Shipping Time Application: Design Elements
14.3 Building the Shipping Time Application: Design Elements
Newly created GroupBox displaying the text Drop Off GroupBoxes 14.3 Building the Shipping Time Application: Design Elements Figure 14.5 GroupBox controls on the Shipping TimeForm. • Add a second Groupbox to contain drop off time
Before clicking inside the GroupBox 14.3 Building the Shipping Time Application: Design Elements Figure 14.6 Adding a Label to a GroupBox. • Place a label inside the GroupBox by clicking the Label tab in the Toolbox
DateTimePicker control before modification (note that the appearance is similar to a ComboBox) 14.3 Building the Shipping Time Application: Design Elements Figure 14.7 DateTimePicker control on the Form. • Add a DateTimePicker to the application by clicking the DateTimePickercontrol in the Toolbox
Up-down arrows for DateTimePicker (note that the appearance is similar to a NumericUpDown control) 14.3 Building the Shipping Time Application: Design Elements Figure 14.8 CustomizedDateTimePicker control on the Form. • Set ShowUpDown property to True
Timer control Component tray 14.3 Building the Shipping Time Application: Design Elements Figure 14.9 Timer control is displayed in the component tray.
14.4 Creating the Shipping Time Application: Inserting Code • Tick event handler • Date property Now retrieves current time • Format output • hh:mm:sstt
Printing the current time 14.4 Creating the Shipping Time Application: Inserting Code Figure 14.10 Inserting code for a Tick event.
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
Storing the current time in dtmCurrentTime 14.4 Creating the Shipping Time Application: Inserting Code Figure 14.11 Storing the current time.
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
Setting the range of drop-off times 14.4 Creating the Shipping Time Application: Inserting Code Figure 14.12 Setting the MinDate and MaxDate properties.
Displaying the delivery time 14.4 Creating the Shipping Time Application: Inserting Code Figure 14.13 Calling the DisplayDeliveryTime procedure.
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
Calculating and displaying the delivery time 14.4 Creating the Shipping Time Application: Inserting Code Figure 14.14 Inserting code in the ValueChanged event handler.
Determining the departure time Calculating the travel time Displaying the delivery time 14.4 Creating the Shipping Time Application: Inserting Code Figure 14.15 Calling the DepartureTimeprocedure.
Declaring variables 14.4 Creating the Shipping Time Application: Inserting Code Figure 14.16 Inserting procedure DepartureTime into the application.
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.17 Determining the seafood shipment’s flight departure time.
Returning the departure time 14.4 Creating the Shipping Time Application: Inserting Code Figure 14.18 Returning the flight departure time.
Displaying the current time ShippingTime.vb(1 of 4)
Setting the DateTimePicker’s minimum and maximum values ShippingTime.vb(2 of 4)
Calculating and displaying the delivery time in Las Vegas Using a Select Case statement to determine departure time ShippingTime.vb(3 of 4)