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.
Practical Learning: Creating the Application
Start Notepad or your text editor and type the following:
Creating the Application
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.
Practical Learning: Creating the Application
Start Notepad or your text editor and type the following:
Creating the Application
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ProgressClock
{
public class Exercise : System.Windows.Forms.Form
{
private System.Windows.Forms.ProgressBar pgrHours;
private System.Windows.Forms.Label lblTime;
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.ProgressBar pgrMinutes;
private System.Windows.Forms.ProgressBar pgrSeconds;
private System.Windows.Forms.Label lblHours;
private System.Windows.Forms.Label lblHour;
private System.Windows.Forms.Label lblMinute;
private System.Windows.Forms.Label lblSecond;
private System.Windows.Forms.Label lblMinutes;
private System.Windows.Forms.Label lblSeconds;
private System.Windows.Forms.Timer ctlTimer;
public Exercise()
{
InitializeComponent();
}
private void InitializeComponent()
{
// lblTime
this.lblTime = new System.Windows.Forms.Label();
this.lblTime.Anchor = ((System.Windows.Forms.AnchorStyles)
((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.lblTime.Location = new System.Drawing.Point(563, 8);
this.lblTime.Size = new System.Drawing.Size(32, 16);
this.lblTime.TabIndex = 6;
this.lblTime.Text = "Time";
// lblHours
this.lblHours = new System.Windows.Forms.Label();
this.lblHours.Location = new System.Drawing.Point(8, 31);
this.lblHours.Size = new System.Drawing.Size(48, 16);
this.lblHours.TabIndex = 0;
this.lblHours.Text = "Hours:";
// pgrHours
this.pgrHours = new System.Windows.Forms.ProgressBar();
this.pgrHours.Location = new System.Drawing.Point(64, 28);
this.pgrHours.Maximum = 23;
this.pgrHours.Size = new System.Drawing.Size(200, 23);
this.pgrHours.Step = 1;
this.pgrHours.TabIndex = 1;
// lblHour
this.lblHour = new System.Windows.Forms.Label();
this.lblHour.Anchor = ((System.Windows.Forms.AnchorStyles)
((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.lblHour.Location = new System.Drawing.Point(568, 32);
this.lblHour.Size = new System.Drawing.Size(24, 16);
this.lblHour.TabIndex = 7;
this.lblHour.Text = "00";
this.lblHour.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
// lblMinutes
this.lblMinutes = new System.Windows.Forms.Label();
this.lblMinutes.Location = new System.Drawing.Point(8, 63);
this.lblMinutes.Size = new System.Drawing.Size(48, 16);
this.lblMinutes.TabIndex = 2;
this.lblMinutes.Text = "Minutes:";
// pgrMinutes
this.pgrMinutes = new System.Windows.Forms.ProgressBar();
this.pgrMinutes.Anchor = ((System.Windows.Forms.AnchorStyles)
(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pgrMinutes.Location = new System.Drawing.Point(64, 60);
this.pgrMinutes.Maximum = 59;
this.pgrMinutes.Size = new System.Drawing.Size(500, 23);
this.pgrMinutes.Step = 1;
this.pgrMinutes.TabIndex = 3;
// lblMinute
this.lblMinute = new System.Windows.Forms.Label();
this.lblMinute.Anchor = ((System.Windows.Forms.AnchorStyles)
((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.lblMinute.Location = new System.Drawing.Point(568, 64);
this.lblMinute.Size = new System.Drawing.Size(24, 16);
this.lblMinute.TabIndex = 8;
this.lblMinute.Text = "00";
this.lblMinute.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
// pgrSeconds
this.pgrSeconds = new System.Windows.Forms.ProgressBar();
this.pgrSeconds.Anchor = ((System.Windows.Forms.AnchorStyles)
(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pgrSeconds.Location = new System.Drawing.Point(64, 92);
this.pgrSeconds.Maximum = 59;
this.pgrSeconds.Size = new System.Drawing.Size(500, 23);
this.pgrSeconds.Step = 1;
this.pgrSeconds.TabIndex = 5;
// lblSeconds
this.lblSeconds = new System.Windows.Forms.Label();
this.lblSeconds.Location = new System.Drawing.Point(8, 96);
this.lblSeconds.Size = new System.Drawing.Size(48, 16);
this.lblSeconds.TabIndex = 4;
this.lblSeconds.Text = "Seconds:";
// lblSecond
this.lblSecond = new System.Windows.Forms.Label();
this.lblSecond.Anchor = ((System.Windows.Forms.AnchorStyles)
((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.lblSecond.Location = new System.Drawing.Point(568, 96);
this.lblSecond.Size = new System.Drawing.Size(24, 16);
this.lblSecond.TabIndex = 9;
this.lblSecond.Text = "00";
this.lblSecond.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
// btnClose
this.btnClose = new System.Windows.Forms.Button();
this.btnClose.Anchor = ((System.Windows.Forms.AnchorStyles)
((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnClose.Location = new System.Drawing.Point(488, 128);
this.btnClose.Name = "btnClose";
this.btnClose.TabIndex = 10;
this.btnClose.Text = "Close";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
// ctlTimer
this.ctlTimer = new System.Windows.Forms.Timer();
this.ctlTimer.Enabled = true;
this.ctlTimer.Interval = 20;
this.ctlTimer.Tick += new System.EventHandler(this.ctlTimer_Tick);
// Form: Exercise
this.ClientSize = new System.Drawing.Size(600, 166);
this.MaximizeBox = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Progressive Clock";
this.Resize += new System.EventHandler(this.Exercise_Resize);
this.Controls.Add(this.lblTime);
this.Controls.Add(this.lblHours);
this.Controls.Add(this.pgrHours);
this.Controls.Add(this.lblHour);
this.Controls.Add(this.lblMinutes);
this.Controls.Add(this.pgrMinutes);
this.Controls.Add(this.lblMinute);
this.Controls.Add(this.lblSecond);
this.Controls.Add(this.pgrSeconds);
this.Controls.Add(this.lblSeconds);
this.Controls.Add(this.btnClose);
}
[STAThread]
static void Main()
{
Application.Run(new Exercise());
}
private void ctlTimer_Tick(object sender, System.EventArgs e)
{
DateTime curTime = DateTime.Now;
int H = curTime.Hour;
int M = curTime.Minute;
int S = curTime.Second;
pgrHours.Value = H;
pgrMinutes.Value = M;
pgrSeconds.Value = S;
lblHour.Text = H.ToString();
lblMinute.Text = M.ToString();
lblSecond.Text = S.ToString();
}
private void Exercise_Resize(object sender, System.EventArgs e)
{
this.pgrHours.Width = 2 * this.pgrMinutes.Width / 5;
}
private void btnClose_Click(object sender, System.EventArgs e)
{
Close();
}
}
- Save the file in a new folder named ProgrClock
- Save the file itself as Exercise.cs in the ProgrClock folder
- Open the Command Prompt and switch to the ProgrClock folder
- To compile the exercise, type csc Exercise.cs and press Enter
- To execute the exercise, type Exercise and press Enter
No comments:
Post a Comment