Practical Learning Logo

Progressive Clock

Introduction

The dialog box on this exercise displays three progress bars: one holds the value of the current hour, another holds the value of the minutes in the current hour, the last displays the seconds of the current minute. We also use a label on the right side of each progress bar to display its corresponding value.

Creating the Controls

To start this application, you can use a form. Then add the necessary controls to it as we will design shortly. Although the hour holds 24 values while the minutes and the seconds hold 60 values each, we will use the same dimensions (especially the same width) for all progress controls. Because a progress control has no mechanism or message to trigger the change of its position, we will use a timer to handle such a message.

Practical Learning: Starting the Exercise

  1. Start a new Visual C# project as a Windows Forms Application
  2. Name it ProgressClock
  3. Change the Form properties as follows:
    FormBorderStyle: FixedDialog
    MaximizeBox: False
    MaximizeBox: False
    Text: Progressive Clock
  4. Design the form as follows:
     
    Control Name Text Other Properties
    Label Time  
    Label   Hour:  
    ProgressBar  pgrHour   Maximum: 23
    Step: 1
    Label lblHours 00  
    Label   Minute:  
    ProgressBar  pgrMinute   Maximum: 59
    Step: 1
    Label lblMinutes 00  
    Label   Second:  
    ProgressBar  pgrSecond   Maximum: 59
    Step: 1
    Label lblSeconds 00  
    Button btnClose Close  
    Timer     Enabled: True
    Interval: 20
  5. Double-click the Time to generate its Tick() event and implement it as follows:
     
    private void timer1_Tick (Object sender, System.EventArgs e)
    	{
    		System.DateTime curTime = System.DateTime.get_Now();
    
    		int H = curTime.get_Hour();
    		int M = curTime.get_Minute();
    		int S = curTime.get_Second();
    
    		pgrHour.set_Value(H);
    		pgrMinute.set_Value(M);
    		pgrSecond.set_Value(S);
    
    		lblHours.set_Text(String.Format("{0}", (System.Int32)H));
    		lblMinutes.set_Text(String.Format("{0}", (System.Int32)M));
    		lblSeconds.set_Text(String.Format("{0}", (System.Int32)S));
    	}
    
  6. On the form, double-click the Close button and implement it as follows:
     
    private void btnClose_Click (Object sender, System.EventArgs e)
    	{
    		Close();
    	}
  7. Test the application
  8. After using it, close it and return to your programming environment.
 

Home Copyright 2004 FunctionX, Inc.