|
Example Application: The Progress Clock |
|
|
This is an example of an application that uses the progress bar
to display the current time. It gets the actual time from the computer.
|
Application:
Creating the Application
|
|
- Start a new Windows Application named ProgressClock1
- Design the form as follows:
|
Control |
Text |
Name |
Other Properties |
Label |
|
Time |
|
Anchor: Right |
Label |
|
Hours: |
|
|
ProgressBar |
|
00 |
|
Anchor: Left, Right |
Label |
|
|
lblHours |
|
Label |
|
Minutes: |
|
|
ProgressBar |
|
|
|
Anchor: Left, Right |
Label |
|
00 |
lblMinutes |
Anchor: Right |
Label |
|
Seconds: |
|
|
ProgressBar |
|
|
|
Anchor: Left, Right |
Label |
|
00 |
lblSeconds |
Anchor: Right |
Button |
|
Close |
btnClose |
Anchor: Right |
Timer |
|
|
|
Enabled: True Interval: 20 |
|
- Change the properties of the progress bar controls as follows:
Name |
Maximum |
Step |
pgrHours |
23 |
1 |
pgrMinutes |
59 |
1 |
pgrSeconds |
59 |
1 |
- Double-click the timer control to generate its Tick event and
implement it as follows:
private void timer1_Tick(object sender, EventArgs e)
{
// Get the current time
DateTime curTime = DateTime.Now;
// Retrieve the hour value of the current time
int H = curTime.Hour;
// Retrieve the minute value of the current time
int M = curTime.Minute;
// Retrieve the second value of the current time
int S = curTime.Second;
// Draw the progress boxes based on the values of the time
pgrHours.Value = H;
pgrMinutes.Value = M;
pgrSeconds.Value = S;
// Display the values in the corresponding labels
lblHours.Text = H.ToString();
lblMinutes.Text = M.ToString();
lblSeconds.Text = S.ToString();
}
- Double-click the Close button and implement it as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
- Execute the application to test it then close the form:
- Close the form and return to your programming environment
- On the form, click the top progress bar
- Press and hold Ctrl
- Click the other two progress bars
- Release Ctrl
- In the Properties window, click Style, click the arrow of its combo
box, and select Continuous
- Execute the application to see the result
- Close the form and return to your programming environment
|
|