192
LESSON 15 Working With Dates anD times
Normally the TimeSpan’s ToString method displays a value in the format d.hh:mm:ss.fffffff.
In this example, you use
string.Format to display the elapsed time in the format hh:mm:ss.ff.
You can download the code and resources for this Try It from the book’s web
page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find
them within the Lesson15 folder.
Lesson Requirements
Create the form shown in Figure 15-1. In addition to the controls that are visible, give the
form a Timer with Interval = 1. Initially disable the Stop button.
When the user clicks the Start button, start the
Timer, disable the Start button, and enable
the Stop button.
When the user clicks the Stop button, stop the
Timer, enable the Start button, and disable the
Stop button.
When the
Timer’s Tick event fires, display the elapsed time in the format hh:mm:ss.ff.
Hints
TimeSpan
doesn’t use the same formatting characters as a DateTime, so for example, you
can’t simply use a format string such as
hh:mm:ss.ff. Instead use the TimeSpan properties
to get the elapsed hours, minutes, seconds, and milliseconds and then format those values.
Step-by-Step
Create the form shown in Figure 15-1. In addition to the controls that are visible, give the
form a Timer with Interval = 1. Initially disable the Stop button.
1. Add the Start and Stop buttons and a Label to the form as shown in Figure 15-1. Set
the Stop button’s
Enabled property to False.
2. Add a Timer and set its Interval property to 1 millisecond. (This is much faster than
your computer can actually fire the
Timer’s Click event, so the Timer will run as
quickly as it can.)
When the user clicks the Start button, start the
Timer, disable the Start button, and enable
the Stop button.
1. To remember the time when the user clicked the Start button, create a DateTime field
named
StartTime.
// The time when the user clicked Start.
private DateTime StartTime;
596906c15.indd 192 4/7/10 12:33:00 PM
Try It
193
2. Add the following code to the Start button’s Click event handler:
// Start the Timer.
private void startButton_Click(object sender, EventArgs e)
{
StartTime = DateTime.Now;
startButton.Enabled = false;
stopButton.Enabled = true;
updateLabelTimer.Enabled = true;
}
When the user clicks the Stop button, stop the
Timer, enable the Start button, and disable the
Stop button.
1. Add the following code to the Stop button’s Click event handler:
// Stop the Timer.
private void stopButton_Click(object sender, EventArgs e)
{
startButton.Enabled = true;
stopButton.Enabled = false;
updateLabelTimer.Enabled = false;
}
When the
Timer’s Tick event fires, display the elapsed time in the format hh:mm:ss.ff.
1. Use code similar to the following. Notice that the code divides the number of millisec-
onds by 10 to convert it into hundredths of seconds.
// Display the elapsed time.
private void updateLabelTimer_Tick(object sender, EventArgs e)
{
// Subtract the start time from the current time
// to get elapsed time.
TimeSpan elapsed = DateTime.Now - StartTime;
// Display the result.
elapsedTimeLabel.Text = string.Format(
“{0:00}:{1:00}:{2:00}.{3:00}“,
elapsed.Hours,
elapsed.Minutes,
elapsed.Seconds,
elapsed.Milliseconds / 10);
}
Please select Lesson 15 on the DVD to view the video that accompanies this lesson.
596906c15.indd 193 4/7/10 12:33:00 PM
Click here to Play
194
LESSON 15 Working With Dates anD times
EXERCISES
1. Make a program with a Birth Date TextBox and a Calculate Button. When the user enters
a birth date and clicks the
Button, calculate the person’s current age and add items to a
ListBox that display the age converted into each of days, hours, minutes, and seconds.
2. Make a program that displays the days of the week for your next 10 birthdays in a ListBox.
3. Make a program with two TextBoxes for dates and a Button. When the user clicks the Button,
the program should calculate the time between the dates and display it in a message box.
4. Modify the program you built for Exercise 3 to use DateTimePicker controls instead of
TextBoxes. To keep things simple, just display the total number of days between the dates. Use
the controls’
Value properties to get the selected dates. (This control prevents the user from
entering invalid dates. Preventing the user from making mistakes is generally a good idea.)
You can download the solutions to these exercises from the book’s web page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find them in
the Lesson15 folder.
596906c15.indd 194 4/7/10 12:33:00 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.17.74