Skip to content

Commit

Permalink
Added C#-WinForms implementation of timer example
Browse files Browse the repository at this point in the history
  • Loading branch information
suvjunmd committed Feb 22, 2015
1 parent 7c93c26 commit 08760bb
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions C#-WinForms/timer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
To compile under Windows run:
csc.exe /target:winexe timer.cs
94 changes: 94 additions & 0 deletions C#-WinForms/timer/timer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Timer
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

int duration = 25;
decimal elapsedTime = 0;

var labelElapsedText = new Label();
labelElapsedText.Location = new Point(12, 22);
labelElapsedText.Text = "Elapsed Time:";
labelElapsedText.AutoSize = true;

var progressBar = new ProgressBar();
progressBar.Bounds = new Rectangle(92, 12, 180, 23);

var labelElapsedValue = new Label();
labelElapsedValue.Location = new Point(12, 51);
labelElapsedValue.Text = "00.0s";
labelElapsedValue.AutoSize = true;

var labelDuration = new Label();
labelDuration.Location = new Point(12, 80);
labelDuration.Text = "Duration:";
labelDuration.AutoSize = true;

var trackBar = new TrackBar();
trackBar.Bounds = new Rectangle(92, 70, 180, 45);
trackBar.TickStyle = TickStyle.None;
trackBar.Maximum = 50;
trackBar.Value = duration;

var button = new Button();
button.Bounds = new Rectangle(15, 121, 257, 23);
button.Text = "Reset";

var timer = new System.Windows.Forms.Timer();

button.Click += delegate { elapsedTime = 0; };
trackBar.Scroll += delegate { duration = trackBar.Value; };

timer.Tick += delegate
{
if (elapsedTime == duration)
{
return;
}

if (elapsedTime > duration)
{
elapsedTime = duration;
}
else
{
elapsedTime += 0.1M;
}

labelElapsedValue.Text = elapsedTime.ToString("00.0") + "s";

if (duration == 0)
{
progressBar.Value = 100;
}
else
{
progressBar.Value = (int)(elapsedTime * 100 / duration);
}
};

timer.Start();

var form = new Form();
form.ClientSize = new Size(287, 155);
form.Text = "Timer";
form.Controls.Add(labelElapsedText);
form.Controls.Add(progressBar);
form.Controls.Add(labelElapsedValue);
form.Controls.Add(labelDuration);
form.Controls.Add(trackBar);
form.Controls.Add(button);

Application.Run(form);
}
}
}

0 comments on commit 08760bb

Please sign in to comment.